Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assert, -NDEBUG and segmentation fault

I have quite a large piece of code, that works well in a development version, with many assert() in the code. I disabled assertions with -DNDEBUG directive passed to g++, and now my code breaks with seg. fault. Is there something I don't know about assert()?

like image 952
Jakub M. Avatar asked May 30 '26 13:05

Jakub M.


1 Answers

The most common issue with assert to my knowledge is having code with side effects within the assert itself. When you compile with -DNDEBUG asserts are essentially commented out, and thus code inside the assert isn't executed. The assert man page mentions this in the bugs section:

BUGS
       assert()  is implemented as a macro; if the expression tested has side-
       effects, program behavior will be different depending on whether NDEBUG
       is defined.  This may create Heisenbugs which go away when debugging is
       turned on.
like image 100
wollw Avatar answered Jun 01 '26 01:06

wollw



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!