The free
function and delete
operator are used exclusively to release dynamically allocated memory. Attempting to release
any other type of memory is undefined behavior.
The following non-heap memory types may not be released:
- Stack allocated memory - local variables or memory allocated with the
alloca
, _alloca
, _malloca
and
__builtin_alloca
functions.
- Executable program code - function pointers.
- Program data - global and static variables.
- Read-only program data - constants and strings.
What is the potential impact
Trying to release non-heap memory using free
or delete
results in undefined behavior.
When a program comprises undefined behavior, the compiler no longer needs to adhere to the language standard, and the program has no meaning
assigned to it.
The application will usually just crash, but in the worst case, the application may appear to execute correctly, while losing data or producing
incorrect results.