Why assert is in the language?

Lutger lutger.blijdestijn at gmail.com
Wed Jun 23 00:25:46 PDT 2010


Ellery Newcomer wrote:

> On 06/22/2010 05:36 PM, Ali Çehreli wrote:
>> Jonathan M Davis wrote:
>>  > Steven Schveighoffer wrote:
>>
>>  >> all calls to assert are removed by the compiler in release mode. I
>> don't
>>  >> think there's a way to implement that via a library (it would be nice
>>  >> though!)
>>
>>  > Also IIRC, the compiler uses assert(0) to ensure that functions blow
>> up at
>>  > runtime if you manage to hit the end of them without a return statement.
>>
>> I just read in TDPL that the assert(0) calls in user code are not
>> removed even in release mode.
>>
>> Ali
> 
> Really?
> 
> // test.d
> void main(){
>   assert(0);
> }
> 
> $ dmd test -release
> $ ./test
> Segmentation fault (core dumped)
> 
> good job, dmd. Can anyone see if anything is going on here?

According to TDPL, assert(false) is treated specially and never removed. It 
serves two purposes:
- portable way of issuing the HLT instruction 
- telling the compiles that everything after is dead code, the compiler  
recognizes the meaning assert(false)


More information about the Digitalmars-d-learn mailing list