Using assert after allocating dynamic memory

Feb 8, 2013 at 7:26pm
When allocating dynamic memory I have learned that it is important to delete the memory allocation before the program ends. What happens to dynamic allocated memory when assert() is used somewhere in the program if the call to assert results in the program exiting?
Feb 8, 2013 at 7:55pm
What happens to dynamic allocated memory when assert() is used

The operating system releases the resources used by the program when the program terminates. The heap is simply part of the program's data space.

By always matching new and delete calls, you create a methodology to prevent memory leaks. This may seem obvious in a simple linear program. Preventing memory leaks becomes very important in complex programs where objects are created and destoryed in a non-linear fashion. If memory leaks exist, it's possible to exhaust the heap and cause new or malloc calls to fail.

Feb 8, 2013 at 8:39pm
It's important to deallocate before the function ends. In many modern operating systems all memory associated with a program is automatically deallocated when the program ends.

If a call to assert results in the program exiting, all allocated memory remains allocated and must be freed by the operating system.
Feb 8, 2013 at 9:05pm
@toum
It's important to deallocate before the function ends

That very much depends on the structure of the program and the lifetime of the object. Objects are not necessarily created and deleted in the same function, especially in a threaded programs.
Feb 8, 2013 at 9:17pm
Thanks!
Topic archived. No new replies allowed.