On 09/04/2008, David Adam david.adam.cnrs@gmail.com wrote:
This would increase the size of the stack exponentially. Would it be better to do this:
Take a stack_size_reference (for instance 32 items) When This->current =32, then increase the size of the stack of stack_size_reference (that is 64 items now) Then when This->current is =64, again increase the size of the stack of stack_size_reference (that is 98 items now), and so on....
This would increase the size of the stack linearly instead of exponentially.
Growing exponentially is intended. If HeapReAlloc can't simply grow the current block, it has to copy the contents over to a new memory location. If the stack becomes larger, the relative time it costs to do the copy becomes more and more expensive compared to the actual push. Growing by a factor instead of by a constant size keeps the amortized running time for a series of pushes constant independent of the the array size. (Not that I expect a matrix stack to become large enough for this to matter a whole lot, but that's the reasoning.)
You should also assign the result of HeapReAlloc() to This->matrix again.
I thought that
HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->matrix, ..........);
do exactly what you say
It can't do that. HeapReAlloc takes a pointer to a memory location, so it can change the memory at that location, but it can't change the pointer itself.