Thread overview
Segfault from changing a string
Apr 24, 2005
brad beveridge
Apr 24, 2005
brad beveridge
April 24, 2005
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
April 24, 2005
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
April 24, 2005
> 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