string literals

"Jérôme M. Berger" jeberger at free.fr
Sat Jan 26 00:44:27 PST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Saaa wrote:
>> 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*

> 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

>> 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.

		Jerome
- --
+------------------------- Jerome M. BERGER ---------------------+
|    mailto:jeberger at free.fr      | ICQ:    238062172            |
|    http://jeberger.free.fr/     | Jabber: jeberger at jabber.fr   |
+---------------------------------+------------------------------+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFHmvLqd0kWM4JG3k8RAhmTAJ0f0SlvCA4JlMiko5wRoarq/2mJkQCfZ/Ix
tc085J5V9KkkxApKLxGeLgE=
=GfMd
-----END PGP SIGNATURE-----


More information about the Digitalmars-d-learn mailing list