Is all this Invarient **** er... stuff, premature optimisation? (on undefined)

Bruno Medeiros brunodomedeiros+spam at com.gmail
Tue Apr 29 13:06:44 PDT 2008


Janice Caron wrote:
> 2008/4/28 Simen Kjaeraas <simen.kjaras at gmail.com>:
>>  int main(string[] args)
>>  {
>>   char[] a = cast(char[])args[0];
>>   a[2..5] = "XXX";
>>   writefln(a);
>>   return 0;
>>  }
>>  This compiles and runs, and seems to do what you describe. Sure, there's a
>>  cast there, but it's not all that bad, is it?
> 
> Yes, it's extremely bad. Casting away invariant is UNDEFINED BEHAVIOR,
> and should never be done.
> 

It's not merely undefined, it's *illegal*!
I hate the C/C++ tradition of calling "undefined behavior" to things 
that are *illegal*. Yes, illegal behavior causes undefined behavior, but 
they're not the same thing. Illegal is something that may cause your 
program to crash, or simply become in a fault and erroneous state. 
Undefined is just undefined. For example, this expression in C:
   a = (x++) + x*2;
has undefined behavior (because of order of evaluation issues). But it's 
not *illegal* behavior, your program will not crash and burn because of 
that.

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list