|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D - To dup or not to dup?
Hi, I'm absolutely new to D and about to see if D is a language I could like. On http://www.digitalmars.com/d/cppstrings.html I found char[] s1 = "hello world"; char[] s2 = "goodbye ".dup; s2[8..13] = s1[6..11]; // s2 is "goodbye world" The .dup is needed because string literals are read-only in D, the .dup will create a copy that is writable. The same with char []a = "Txst"; a[1] = 'a'; I find that interesting because here it also works (compiles and runs) without .dup (DMD 1.007). Has the language changed since writing that web page? Jürgen Feb 25 2007
Jürgen Herz wrote:Hi, I'm absolutely new to D and about to see if D is a language I could like. On http://www.digitalmars.com/d/cppstrings.html I found char[] s1 = "hello world"; char[] s2 = "goodbye ".dup; s2[8..13] = s1[6..11]; // s2 is "goodbye world" The .dup is needed because string literals are read-only in D, the .dup will create a copy that is writable. The same with char []a = "Txst"; a[1] = 'a'; I find that interesting because here it also works (compiles and runs) without .dup (DMD 1.007). Has the language changed since writing that web page? Jürgen Feb 25 2007
Jürgen Herz wrote:Hi, I'm absolutely new to D and about to see if D is a language I could like. On http://www.digitalmars.com/d/cppstrings.html I found char[] s1 = "hello world"; char[] s2 = "goodbye ".dup; s2[8..13] = s1[6..11]; // s2 is "goodbye world" The .dup is needed because string literals are read-only in D, the .dup will create a copy that is writable. The same with char []a = "Txst"; a[1] = 'a'; I find that interesting because here it also works (compiles and runs) without .dup (DMD 1.007). Has the language changed since writing that web page? Feb 25 2007
Frits van Bommel wrote:Jürgen Herz wrote:The same with char []a = "Txst"; a[1] = 'a'; I find that interesting because here it also works (compiles and runs) without .dup (DMD 1.007). Has the language changed since writing that web page? Feb 26 2007
Jürgen Herz wrote:Frits van Bommel wrote:This isn't enforced on Windows. I'm not sure whose fault that is, but I assume Microsoft :P. Before you think I'm a MS basher, I'd like to mention that IIRC it's not enforced for C either on Windows: if you initialize char* with a string literal and try to modify it -- which is illegal in C for the very same reason -- it won't complain. Feb 26 2007
Frits van Bommel wrote:Jürgen Herz wrote:Frits van Bommel wrote:This isn't enforced on Windows. I'm not sure whose fault that is, but I assume Microsoft :P. Before you think I'm a MS basher, I'd like to mention that IIRC it's not enforced for C either on Windows: if you initialize char* with a string literal and try to modify it -- which is illegal in C for the very same reason -- it won't complain. Feb 26 2007
Lionello Lunesu wrote:Frits van Bommel wrote:Jürgen Herz wrote:BTW const char []a = "Test".dup; gives consttest.d(3): Error: cannot evaluate _adDupT(&_D12TypeInfo_G4a6__initZ,"Test") at compile time. What does in want to tell me? Feb 26 2007
Frits van Bommel wrote:Jürgen Herz wrote:It also isn't enforced for C on Linux (gcc) though it also doesn't crash. Anyways, that's C and D (resp. dmd) could do better. Feb 27 2007
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. Feb 26 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. Feb 26 2007
|