My stackAlloc
function looks like this:
void* stackAlloc(size_t size) {
if (size > maxStackAllocation)
return malloc(size);
else
return _alloca(size);
}
void stackAllocFree(void *ptr, size_t size) {
if (size > maxStackAllocation) {
free(ptr);
}
}
If I change so the stackAlloc
function always use malloc
instead of alloca
everything works.
I changed the function to a macro, and now its working as expected:
#define maxStackAllocation 1024
#define stackAlloc(size) \
( \
(size > maxStackAllocation)? \
malloc(size): \
_alloca(size) \
)
#define stackAllocFree(ptr, size) \
( \
(size > maxStackAllocation)? \
free(ptr): \
void() \
)
Assuming you're running on Windows, since your code calls _alloca()
, per the MSDN documentation:
_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function exits
Note that the memory is freed when the calling function exits - which I'm assuming also means the calling function returns.
Your code:
void* stackAlloc(size_t size) {
if (size > maxStackAllocation)
return malloc(size);
else
return _alloca(size);
}
returns, thus freeing the memory obtained via _alloca()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With