assert(false, "...") doesn't terminate program?!

Don Clugston dac at nospam.com
Tue Oct 30 05:53:54 PDT 2012


On 29/10/12 18:38, Walter Bright wrote:
> On 10/29/2012 7:51 AM, Don Clugston wrote:> On 27/10/12 20:39, H. S.
> Teoh wrote:
>  >> On Sat, Oct 27, 2012 at 08:26:21PM +0200, Andrej Mitrovic wrote:
>  >>> On 10/27/12, H. S. Teoh <hsteoh at quickfur.ath.cx> wrote:
>  >>>>          writeln("how did the assert not trigger??!!");    // how
> did we get
>  >>>> here?!
>  >>>
>  >>> Maybe related to -release?
>  >> [...]
>  >>
>  >> Haha, you're right, the assert is compiled out because of -release.
>  >>
>  >> But I disassembled the code, and didn't see the "auto x = 1/toInt()"
>  >> either. Is the compiler optimizing that away?
>  >
>  > Yes, and I don't know on what basis it thinks it's legal to do that.
>
> Because x is a dead assignment, and so the 1/ is removed.


> Divide by 0 faults are not considered a side effect.

Ah, that's interesting, I didn't know that.



I think the code would be
> better written as:
>
>      if (toInt() == 0) throw new Error();
>
> If you really must have a divide by zero fault,
>
>      if (toInt() == 0) divideByZero();
>
> where:
>
>      void divideByZero()
>      {
>           static int x;
>           *cast(int*)0 = x / 0;
>      }

And that works because writes to 0 _are_ considered a side-effect?
Is that guaranteed to work?




More information about the Digitalmars-d mailing list