Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert frees memory in C++

Suppose we have a program where we allocate some memory and then we have an assert statement some lines after. If the assert statement throws and error, what happens with the allocated memory? Does it get free before stopping the program?

like image 720
BRabbit27 Avatar asked Dec 16 '25 23:12

BRabbit27


1 Answers

assert on failure, writes the error to stderr and calls abort(). which unlike exit() doesn't execute the functions registered with atexit(), nor does it call destructors.

Hence, none of your destructors, clean-up code etc can be called. So it is up to the OS, as the memory isn't freed by the program, before its "unexpected" termination.


This is probably by design, as calling destructors might result in some further error. It terminates at that failed assert, executing no further code.

like image 125
Anirudh Ramanathan Avatar answered Dec 19 '25 13:12

Anirudh Ramanathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!