[OCLUG-devel] strcpy segmentation faults

Dan Stromberg strombrg at dcs.nac.uci.edu
Fri Jun 24 12:17:25 PDT 2005


On Fri, 2005-06-24 at 12:02 -0700, Morgan Simpson wrote:
> On Jun 23, 2005, at 11:01 PM, Doug Jolley wrote:
> 
> > Do you see a difference between:
> >        char myVar[] = "My dog has fleas.";
> > and
> >        const myVar char[] = "My dog has fleas.";
> 
> In the second case, the compiler will (should) issue an error if you 
> use "myVar" as an lvalue in an expression. A bigger issue arises with:
> 
>    char *myVar  = "My dog has fleas."; // example 1
>    char myVar[] = "My dog has fleas."; // example 2
> 
> If you attempt to modify the string in example 1, you should get a 
> segmentation fault because the GCC compiler stored the string in a 
> read-only segment of memory. This problem does not occur with example 2 
> as the string resides in a writable segment of memory. If you want to 
> safely modify the string in example 1, you need to use the GCC flag 
> -fwritable-strings.

I gather that gcc 4.0.0 drops support of the -fwritable-strings flag,
but prior versions of gcc should have it - though I'm not 100% sure that
it will work on all platforms that gcc supports.




More information about the OCLUG-devel mailing list