www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Segfault from changing a string

reply brad beveridge <brad nowhere.com> writes:
The following segfaults on Linux dmd - should it?

char[] test = "this is a test".dup; is OK

int main(char[][] args)
{
     char[] test = "this is a test";
     test[0] = 'T';
     return 0;
}

Brad
Apr 24 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
brad beveridge wrote:

 The following segfaults on Linux dmd - should it?
Yes, as it has read-only str literals... This is mentioned in the very small print on the site: http://www.digitalmars.com/d/dcompiler.html#linux "Differences from Win32 version * String literals are read-only. Attempting to write to them will cause a segment violation. * The configuration file is /etc/dmd.conf"
 char[] test = "this is a test".dup; is OK
This is the "correct" D code to use... Strings should use Copy-On-Write (COW) This second way will work with GDC too. --anders
Apr 24 2005
parent brad beveridge <brad nowhere.com> writes:
 This is mentioned in the very small print on the site:
 
 http://www.digitalmars.com/d/dcompiler.html#linux
 "Differences from Win32 version
  * String literals are read-only. Attempting to write to
    them will cause a segment violation.
  *  The configuration file is /etc/dmd.conf"
 
 char[] test = "this is a test".dup; is OK
This is the "correct" D code to use... Strings should use Copy-On-Write (COW) This second way will work with GDC too. --anders
Ah, that fine print does look familiar, but it's been a while since I read the whole D spec :) Thank you Anders Brad
Apr 24 2005