digitalmars.D.learn - string literals
- Saaa (3/3) Jan 23 2008 Can somebody please explain to me why char arrays are a special case of
- downs (4/10) Jan 23 2008 I don't understand what you mean with "special case". Character arrays (...
- Saaa (2/7) Jan 23 2008
- downs (3/12) Jan 23 2008 http://digitalmars.com/d/1.0/arrays.html#strings
- Saaa (7/19) Jan 24 2008 I'm sorry about the late reply..
- bearophile (13/15) Jan 24 2008 That's an interesting question. Now and then I too feel the need of muta...
- Saaa (4/35) Jan 24 2008 Thanks, so the common use of strings is such that it is best to make the...
- bearophile (6/7) Jan 25 2008 What's the .access property of arrays in the language you talk about?
- Christopher Wright (3/8) Jan 25 2008 Basically runtime const, as far as I can tell. You can fake it with a
- Saaa (6/17) Jan 25 2008 Didn't they both have immutable strings?
- Robert Fraser (7/10) Jan 25 2008 On UNIX systems, string literals are stored in the code segment. Thus,
- Saaa (7/14) Jan 25 2008 If you load a file into a char[][], will the file be stored in the code
- bearophile (6/12) Jan 25 2008 In practice, there is. In D 1.x string literals are, dynamic ones aren't...
- Saaa (4/16) Jan 25 2008 from the 1.0 documentation:char[] str;
- Saaa (5/5) Jan 25 2008 from the 1.0 documentation:
- Robert Fraser (7/16) Jan 26 2008 Yes, because the "abc" is a string literal, that is to say it's written
- Saaa (3/18) Jan 26 2008 I finally see what a string literal means, but this code still bothers m...
- Simen Kjaeraas (14/39) Jan 26 2008 en =
- bearophile (8/20) Jan 26 2008 I am no an expert of D yet, but I think in the following D 1.x code str ...
- Milke Wey (2/25) Jan 26 2008 On linux you would get an nice Segfault when running that code.
- bearophile (8/9) Jan 26 2008 Ah, thank you then. I have thought that's true only for string literals ...
- Milke Wey (4/16) Jan 27 2008 I don't know why but it actually works when using a static array.
- Robert Fraser (4/19) Jan 27 2008 On a UNIX system or a Windows system? It should work fine on Windows
- Milke Wey (4/24) Jan 28 2008 On Linux with DMD 1.025.
- =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= (20/39) Jan 26 2008 -----BEGIN PGP SIGNED MESSAGE-----
- Saaa (6/31) Jan 26 2008 OK, now I know what I didn't get
- Bill Baxter (4/17) Jan 23 2008 That plus there is special casing in the compiler to support
Can somebody please explain to me why char arrays are a special case of arrays? (when is this useful?)
Jan 23 2008
Saaa wrote:Can somebody please explain to me why char arrays are a special case of arrays? (when is this useful?)I don't understand what you mean with "special case". Character arrays (a.k.a. strings) are arrays just like the rest of 'em :) The only difference is that they have an additional literal constructor in the form of "foo". --downs
Jan 23 2008
I meant why are they read only?I don't understand what you mean with "special case". Character arrays (a.k.a. strings) are arrays just like the rest of 'em :) The only difference is that they have an additional literal constructor in the form of "foo".Where can I read about this?--downs
Jan 23 2008
Saaa wrote:I meant why are they read only?http://digitalmars.com/d/1.0/arrays.html#strings --downsI don't understand what you mean with "special case". Character arrays (a.k.a. strings) are arrays just like the rest of 'em :) The only difference is that they have an additional literal constructor in the form of "foo".Where can I read about this?
Jan 23 2008
I'm sorry about the late reply.. I read it, but still don't really know what you mean additional literal constructor. But what I really meant was this: String literals are immutable (read only). Why is this? (I expect there to be a nice reason for it, I just down't know it :)I meant why are they read only?http://digitalmars.com/d/1.0/arrays.html#strings --downsI don't understand what you mean with "special case". Character arrays (a.k.a. strings) are arrays just like the rest of 'em :) The only difference is that they have an additional literal constructor in the form of "foo".Where can I read about this?
Jan 24 2008
Saaa:Why is this? (I expect there to be a nice reason for it, I just down't know it :)<That's an interesting question. Now and then I too feel the need of mutable stings, to change their chars, etc, but here Josh Block and the famous Sedgewick explain why immutables are often good: http://www.cs.princeton.edu/introcs/33design/ << An immutable data type is a data type such that the value of an object never changes once constructed. Examples: Complex and String. When you pass a String to a method, you don't have to worry about that method changing the sequence of characters in the String. On the other hand, when you pass an array to a method, the method is free to change the elements of the array. Immutable data types have numerous advantages. they are easier to use, harder to misuse, easier to debug code that uses immutable types, easier to guarantee that the class variables remain in a consistent state (since they never change after construction), no need for copy constructor, are thread-safe, work well as keys in symbol table, don't need to be defensively copied when used as an instance variable in another class. Disadvantage: separate object for each value. Josh Block, a Java API architect, advises that "Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, you should still limit its mutability as much as possible."Something more: - Immutable data works well with multiprocessing - it's common in functional style programming allowing more pure functions - the GC can manage immutable strings/objects in an efficient enough way. Bye, bearophile
Jan 24 2008
Thanks, so the common use of strings is such that it is best to make the default setting 'immutable'. Then, how about adding the .access property to arrays? (or all types :) With strings default .access as read only?Why is this? (I expect there to be a nice reason for it, I just down't know it :)<That's an interesting question. Now and then I too feel the need of mutable stings, to change their chars, etc, but here Josh Block and the famous Sedgewick explain why immutables are often good: http://www.cs.princeton.edu/introcs/33design/ << An immutable data type is a data type such that the value of an object never changes once constructed. Examples: Complex and String. When you pass a String to a method, you don't have to worry about that method changing the sequence of characters in the String. On the other hand, when you pass an array to a method, the method is free to change the elements of the array. Immutable data types have numerous advantages. they are easier to use, harder to misuse, easier to debug code that uses immutable types, easier to guarantee that the class variables remain in a consistent state (since they never change after construction), no need for copy constructor, are thread-safe, work well as keys in symbol table, don't need to be defensively copied when used as an instance variable in another class. Disadvantage: separate object for each value. Josh Block, a Java API architect, advises that "Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, you should still limit its mutability as much as possible."Something more: - Immutable data works well with multiprocessing - it's common in functional style programming allowing more pure functions - the GC can manage immutable strings/objects in an efficient enough way. Bye, bearophile
Jan 24 2008
Saaa:Then, how about adding the .access property to arrays? (or all types :) With strings default .access as read only?<What's the .access property of arrays in the language you talk about? If you want to try the D language, you may want to use the 1.x series instead of the 2.x that is alpha and has immutable strings. Immutable things (like strings) are useful as AA keys too. In D 1.x (and Ruvy too, maybe) you can use a string as key, but if you later change that string its hash function isn't automatically recomputed, this leads to chaos. In Python you have both mutable and immutable arrays (called list and tuple), strings are immutable, but with the standard module "array" you can use mutable "strings" too. Python AAs called dict accept only their immutable versions to avoid those bugs (and inside tuples you have to put immutables). Bye, bearophile
Jan 25 2008
bearophile wrote:Saaa:Basically runtime const, as far as I can tell. You can fake it with a class easily enough.Then, how about adding the .access property to arrays? (or all types :) With strings default .access as read only?<What's the .access property of arrays in the language you talk about?
Jan 25 2008
What's the .access property of arrays in the language you talk about?It was a suggestion :)If you want to try the D language, you may want to use the 1.x series instead of the 2.x that is alpha and has immutable strings.Didn't they both have immutable strings? I'm more in search of a way to make char[] not immutable anymore, for most things I do its only a hassle.Immutable things (like strings) are useful as AA keys too. In D 1.x (and Ruvy too, maybe) you can use a string as key, but if you later change that string its hash function isn't automatically recomputed, this leads to chaos. In Python you have both mutable and immutable arrays (called list and tuple), strings are immutable, but with the standard module "array" you can use mutable "strings" too. Python AAs called dict accept only their immutable versions to avoid those bugs (and inside tuples you have to put immutables).Sounds sound to me. This was what I suggested, use a .access property and only accept the 'read-only' for things like that.
Jan 25 2008
Saaa wrote:Didn't they both have immutable strings? I'm more in search of a way to make char[] not immutable anymore, for most things I do its only a hassle.On UNIX systems, string literals are stored in the code segment. 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 .
Jan 25 2008
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? But shouldn't there be an way (per variable) to force the compiler to not store it like that?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.
Jan 25 2008
Saaa:If you load a file into a char[][], will the file be stored in the code segment?Nope.But shouldn't there be an way (per variable) to force the compiler to not store it like that?In practice, there is. In D 1.x string literals are, dynamic ones aren't.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.In D.1.x dyn arrays are mutable. Bye, bearophile
Jan 25 2008
from the 1.0 documentation:char[] str; char[] str1 = "abc"; str[0] = 'b'; // error, "abc" is read only, may crash Is this example correct?Saaa:If you load a file into a char[][], will the file be stored in the code segment?Nope.But shouldn't there be an way (per variable) to force the compiler to not store it like that?In practice, there is. In D 1.x string literals are, dynamic ones aren't.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.In D.1.x dyn arrays are mutable. Bye, bearophile
Jan 25 2008
from the 1.0 documentation: char[] str; char[] str1 = "abc"; str[0] = 'b'; // error, "abc" is read only, may crash Is this example correct?
Jan 25 2008
Saaa wrote:from the 1.0 documentation: char[] str; char[] str1 = "abc"; str[0] = 'b'; // error, "abc" is read only, may crash Is this example correct?Yes, because the "abc" is a string literal, that is to say it's written in the code itself. If str1 was loaded from an outside source, such as a file, user input, etc., then you could modify it without issue. For string LITERALS (a string literal is one you write in the code itself, usually encased in double-quotes), modifying them without calling .dup on them is bad. For other strings, it's perfectly okay.
Jan 26 2008
I finally see what a string literal means, but this code still bothers me. What does changing a dynamic char[] (str) have to do with the string literal in str1from the 1.0 documentation: char[] str; char[] str1 = "abc"; str[0] = 'b'; // error, "abc" is read only, may crash Is this example correct?Yes, because the "abc" is a string literal, that is to say it's written in the code itself. If str1 was loaded from an outside source, such as a file, user input, etc., then you could modify it without issue. For string LITERALS (a string literal is one you write in the code itself, usually encased in double-quotes), modifying them without calling .dup on them is bad. For other strings, it's perfectly okay.
Jan 26 2008
Saaa <empty needmail.com> wrote:I finally see what a string literal means, but this code still bothers==me. What does changing a dynamic char[] (str) have to do with the string =literal in str1en =from the 1.0 documentation: char[] str; char[] str1 =3D "abc"; str[0] =3D 'b'; // error, "abc" is read only, may crash Is this example correct?Yes, because the "abc" is a string literal, that is to say it's writt=in the code itself. If str1 was loaded from an outside source, such as a=file, user input, etc., then you could modify it without issue. For string LITERALS (a string literal is one you write in the code =p =itself, usually encased in double-quotes), modifying them without calling .du=You're right, of course. str[0] has nothing to do with "abc" or str1. Th= e = code will fail, but with an arraybounds error (str.length =3D=3D 0, so n= o = element 0 is available) Now, this code: char[] str1 =3D "abc"; str1[0] =3D 'b'; // error, "abc" is read only, may crash Should fail for the above reasons. Simen Kjaeraason them is bad. For other strings, it's perfectly okay.
Jan 26 2008
Robert Fraser:Saaa wrote:I am no an expert of D yet, but I think in the following D 1.x code str is a dynamic array, so it can be changed safely: void main() { char[] str = "abc"; str[0] = 'b'; } Bye, bearophilechar[] str; char[] str1 = "abc"; str[0] = 'b'; // error, "abc" is read only, may crash Is this example correct?Yes, because the "abc" is a string literal, that is to say it's written in the code itself. If str1 was loaded from an outside source, such as a file, user input, etc., then you could modify it without issue. For string LITERALS (a string literal is one you write in the code itself, usually encased in double-quotes), modifying them without calling .dup on them is bad. For other strings, it's perfectly okay.
Jan 26 2008
On Sat, 2008-01-26 at 10:26 -0500, bearophile wrote:Robert Fraser:On linux you would get an nice Segfault when running that code.Saaa wrote:I am no an expert of D yet, but I think in the following D 1.x code str is a dynamic array, so it can be changed safely: void main() { char[] str = "abc"; str[0] = 'b'; } Bye, bearophilechar[] str; char[] str1 = "abc"; str[0] = 'b'; // error, "abc" is read only, may crash Is this example correct?Yes, because the "abc" is a string literal, that is to say it's written in the code itself. If str1 was loaded from an outside source, such as a file, user input, etc., then you could modify it without issue. For string LITERALS (a string literal is one you write in the code itself, usually encased in double-quotes), modifying them without calling .dup on them is bad. For other strings, it's perfectly okay.
Jan 26 2008
Milke Wey:On linux you would get an nice Segfault when running that code.Ah, thank you then. I have thought that's true only for string literals assigned to static arrays: void main() { char[3] str = "abc"; str[0] = 'b'; } Bye, bearophile
Jan 26 2008
On Sat, 2008-01-26 at 15:26 -0500, bearophile wrote:Milke Wey:I don't know why but it actually works when using a static array. -- Mike WeyOn linux you would get an nice Segfault when running that code.Ah, thank you then. I have thought that's true only for string literals assigned to static arrays: void main() { char[3] str = "abc"; str[0] = 'b'; } Bye, bearophile
Jan 27 2008
Milke Wey wrote:On Sat, 2008-01-26 at 15:26 -0500, bearophile wrote:On a UNIX system or a Windows system? It should work fine on Windows anyway. Also, the static array declaration may say to the compiler "allocate this on the stack"... not sure; I don't think that's speced.Milke Wey:I don't know why but it actually works when using a static array.On linux you would get an nice Segfault when running that code.Ah, thank you then. I have thought that's true only for string literals assigned to static arrays: void main() { char[3] str = "abc"; str[0] = 'b'; } Bye, bearophile
Jan 27 2008
On Sun, 2008-01-27 at 22:44 -0800, Robert Fraser wrote:Milke Wey wrote:On Linux with DMD 1.025. -- Mike WeyOn Sat, 2008-01-26 at 15:26 -0500, bearophile wrote:On a UNIX system or a Windows system? It should work fine on Windows anyway. Also, the static array declaration may say to the compiler "allocate this on the stack"... not sure; I don't think that's speced.Milke Wey:I don't know why but it actually works when using a static array.On linux you would get an nice Segfault when running that code.Ah, thank you then. I have thought that's true only for string literals assigned to static arrays: void main() { char[3] str = "abc"; str[0] = 'b'; } Bye, bearophile
Jan 28 2008
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Saaa wrote:No, but then it's not a string *literal*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?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 heapBecause by definition a *dynamic* whatever can be modified. Jerome - -- +------------------------- Jerome M. BERGER ---------------------+ | mailto:jeberger free.fr | ICQ: 238062172 | | http://jeberger.free.fr/ | Jabber: jeberger jabber.fr | +---------------------------------+------------------------------+ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFHmvLqd0kWM4JG3k8RAhmTAJ0f0SlvCA4JlMiko5wRoarq/2mJkQCfZ/Ix tc085J5V9KkkxApKLxGeLgE= =GfMd -----END PGP SIGNATURE-----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.
Jan 26 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),I get it now :)No, but then it's not a string *literal*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?Good to know.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 heapBecause by definition a *dynamic* whatever can be modified.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.
Jan 26 2008
downs wrote:Saaa wrote:That plus there is special casing in the compiler to support foreach(dchar x; some_string) {...} --bbCan somebody please explain to me why char arrays are a special case of arrays? (when is this useful?)I don't understand what you mean with "special case". Character arrays (a.k.a. strings) are arrays just like the rest of 'em :) The only difference is that they have an additional literal constructor in the form of "foo". --downs
Jan 23 2008