string literals

Saaa empty at needmail.com
Sat Jan 26 07:19:21 PST 2008


OK, now I know what I didn't get

Robert Frazier:
LITERALS (a string literal is one you write in the code
itself, usually encased in double-quotes),

>>> On UNIX systems, string literals are stored in the code segment.
>> If you load a file into a char[][], will the file be stored in the code
>> segment?
>> Wouldn't you normally want to edit it after loading?
>>
> No, but then it's not a string *literal*
I get it now :)

>
>> But shouldn't there be an way (per variable) to force the compiler to not
>> store it like that?
>>
> There is in D 1.0:
>
> char[] foo = "abc";     // Store it in the code segment
> char[] bar = "abc".dup; // Store it on the heap
Good to know.

>
>>> Thus, modifying them will cause a segfault. In D 1.0, the compiler will
>>> let you modify it, but the OS won't. In D 2.0, this rule is enforced by
>>> the compiler. Even on Windows, modifying string literals is probably a 
>>> bad
>>> idea.
>>>
>>> To allow a string literal to be modified, copy it onto the heap with a
>>> .dup .
>>
>> I know this, although it just says it creates a dynamic array. How do you
>> know that the .dup dynamic array is not read-only.
>>
> Because by definition a *dynamic* whatever can be modified.




More information about the Digitalmars-d-learn mailing list