Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The assert statement does not work in c++ [closed]

Tags:

c++

I have experienced a strange problem when using assert in my program. The program does not terminate even when I add a line of codeassert(false). But the assert works when I write several lines of sample code. Anybody know why it happened?

like image 638
Dingbao Xie Avatar asked Mar 11 '26 16:03

Dingbao Xie


2 Answers

If you have:

#define NDEBUG

this turns all assert's into nop's.

like image 74
Paul Evans Avatar answered Mar 13 '26 06:03

Paul Evans


If you have differing behaviour, depending on the amount of code, then I guess you don't have NDEBUG defined and I would guess the compiler is simply compiling out the redundant code.

More details about environment are required, however, you give a definitive answer.

like image 28
trojanfoe Avatar answered Mar 13 '26 05:03

trojanfoe