immutable string literal?

strtr strtr at spam.com
Mon Feb 22 05:32:20 PST 2010


Daniel Keep Wrote:

> 
> strtr wrote:
> > On winXP (D1) I can compile/run this code without a problem.
> > Not even a warning.
> > 
> > void main() {
> >   char[] s= "immutable literal?";
> >   s[$-1] = '!';
> >   writefln(s);
> > } 
> > Codepad runs into a segmentation fault.
> > http://codepad.org/NQfsRoR5
> > 
> > Why doesn't it result in a compiler error or warning?
> > If it did I would have noticed this quirk earlier.
> 
> There's no compiler error because D1 doesn't have a const/immutable system.
> 
> There's no crash because Windows doesn't write-protect the data segment
> which contains the literal.

But according to the specs, it does constitute an error and I suspect string literals are placed in a specific memory location.
Wouldn't it be possible to error on such code? 



More information about the Digitalmars-d-learn mailing list