Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to _freea really necessary?

I am developping on Windows with DevStudio, in C/C++ unmanaged.

I want to allocate some memory on the stack instead of the heap because I don't want to have to deal with releasing that memory manually (I know about smart pointers and all those things. I have a very specific case of memory allocation I need to deal with), similar to the use of A2W() and W2A() macros.

_alloca does that, but it is deprecated. It is suggested to use malloca instead. But _malloca documentation says that a call to ___freea is mandatory for each call to _malloca. It then defeats my purpose to use _malloca, I will use malloc or new instead.

Anybody knows if I can get away with not calling _freea without leaking and what the impacts are internally?

Otherwise, I will end-up just using deprecated _alloca function.

like image 539
Philibert Perusse Avatar asked Apr 09 '09 15:04

Philibert Perusse


People also ask

What happens if you don't free in C?

If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).

What happens if you free unallocated memory?

Freeing an unallocated pointer will cause undefined behavior in the program.

What happens when you don't free dynamically allocated memory?

If dynamically allocated memory is not freed, it results in a memory leak and system will run out of memory. This can lead to program crashing.

What happens when you call free twice?

Calling free() twice on the same value can lead to memory leak. When a program calls free() twice with the same argument, the program's memory management data structures become corrupted and could allow a malicious user to write values in arbitrary memory spaces.


1 Answers

It is always important to call _freea after every call to _malloca.

_malloca is like _alloca, but adds some extra security checks and enhancements for your protection. As a result, it's possible for _malloca to allocate on the heap instead of the stack. If this happens, and you do not call _freea, you will get a memory leak.

In debug mode, _malloca ALWAYS allocates on the heap, so also should be freed.

Search for _ALLOCA_S_THRESHOLD for details on how the thresholds work, and why _malloca exists instead of _alloca, and it should make sense.


Edit:

There have been comments suggesting that the person just allocate on the heap, and use smart pointers, etc.

There are advantages to stack allocations, which _malloca will provide you, so there are reasons for wanting to do this. _alloca will work the same way, but is much more likely to cause a stack overflow or other problem, and unfortunately does not provide nice exceptions, but rather tends to just tear down your process. _malloca is much safer in this regard, and protects you, but the cost is that you still need to free your memory with _freea since it's possible (but unlikely in release mode) that _malloca will choose to allocate on the heap instead of the stack.

If your only goal is to avoid having to free memory, I would recommend using a smart pointer that will handle the freeing of memory for you as the member goes out of scope. This would assign memory on the heap, but be safe, and prevent you from having to free the memory. This will only work in C++, though - if you're using plain ol' C, this approach will not work.

If you are trying to allocate on the stack for other reasons (typically performance, since stack allocations are very, very fast), I would recommend using _malloca and living with the fact that you'll need to call _freea on your values.

like image 170
Reed Copsey Avatar answered Sep 25 '22 15:09

Reed Copsey