Assert and undefined behavior

kdevel kdevel at vogtner.de
Thu Oct 12 21:22:29 UTC 2017


On Thursday, 12 October 2017 at 20:27:03 UTC, Jonathan M Davis 
wrote:
> On Thursday, October 12, 2017 20:15:41 kdevel via
>> ---
>> void main ()
>> {
>>     assert (false);
>> }
>> ---
>>
>> qualifies as "invalid, and therefore has undefined behaviour." 
>> A statement, which makes no sense to me. Either it is a 
>> "debugging aid", that implies defined behavior, or it is 
>> undefined behavior, then assert (false) cannot aid debugging.
>
> assert(false) is a bit special in that it's never removed (it 
> becomes a HLT instruction with -release),

Confirmed. I should have written something like this instead:

---
import std.stdio;
import std.string;
import std.conv;
void main ()
{
    int i;
    i = readln.chomp.to!int;
    assert (i != 3);
    writeln ("i = <", i, ">");
}
---

Is it defined that this program throws an AssertError in debug 
mode if 3 is fed to stdin? If not, assert (...) could not aid 
debugging.


More information about the Digitalmars-d-learn mailing list