To dup or not to dup?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon Feb 26 07:37:29 PST 2007


Saaa wrote:
>> I understand that it is and why it is illegal. And crashing on Linux I'm
>> relieved seeing the "right" consequence of doing illegal things.
>>
> 
> I don't :)
> Could anybody pls explain it?
> What is wrong with changing a part of an array?
> or better, what is the difference between a string literal and an array of 
> chars? 

A string literal directly references the memory it's stored in in the 
loaded executable. That same piece of memory can be used by every 
reference to that string literal. So something like:
---
void main() {
     char[] test = "Hi";
     test[1] = 'o';
     writefln("%s", "Hi");
}
---

may print "Ho" if the string is allowed to be modified. That's also 
dependent on how well the compiler optimizes this stuff, though.



More information about the Digitalmars-d mailing list