File, line and message for assert(0)?

simendsjo simen.endsjo at pandavre.com
Sat May 28 12:14:45 PDT 2011


On 28.05.2011 19:56, Michel Fortin wrote:
> On 2011-05-28 12:32:59 -0400, simendsjo <simen.endsjo at pandavre.com> said:
>
>> I don't think I understand the use case for assert(0) then.. I thought
>> it was just a way getting assert in release mode.
>
> The use case for assert(0) is the same as any assert(whatever): checking
> for things that shouldn't happen. Normally compiling in release mode
> would strip all the asserts to make things slimmer and faster. But given
> there is no cost in checking for assert(0), those are not stripped, they
> are just replaced with a halt instruction. So the assert's still there,
> but you don't have a message for it nor the line number.
>
> If you want to see where the assertion happens, you can either compile
> in non-release mode or you can hook a debugger to your release
> executable and wait for the assert to happen.
>

Thanks to both.

I got confused about by Vladimir's comment: "The optimization and code 
generation phases of compilation may assume that it is unreachable code."

I tested this now, and the compiler does indeed complain about 
unreachable code.

And the debugger also breaks, so it makes more sense now :)


More information about the Digitalmars-d-learn mailing list