I'm using an obscure language that doesn't have a native stack type so I've implemented my own. Now, reading on the net i've found a few different approaches to do this.
This is my implementation (psuedo code)
//push method
function Push(int)
{
Increase (realloc) stack by 4 bytes;
Push int into the new memory area;
}
//pop method
function Pop()
{
collect int from the top of the stack;
reallocate (realloc) the stack to shrink it by 4 bytes;
return int;
}
Now some people say that using a realloc() call to resize the stack after popping a value is bad for performance so i have a few questions:
- Is it best to just grow the stack using malloc then free it at program end?
- To resize the stack (push) is it best to increase by 4 bytes or more?
- Is it best practise to increase the stack by doubling the memory allocated when it's filled?
- What are your thoughts of the above code?