This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Help wanted


Hi, Ben:

It works!

Thanks!
 
Jane


-----Original Message-----
From: Ben Davis [mailto:bnd25 at cam dot ac dot uk] 
Sent: Monday, March 17, 2003 10:54 AM
To: Jane Liang; 'GCC-help'
Subject: Re: Help wanted


On Monday 17 March 2003 4:26 pm, Jane Liang wrote:
> Line 8      char *str = "0.12000, 0.0, 0.0";

This is a constant string, so it is stored in read-only memory.

> Line 13     *ptr = '\0';

You are now writing to read-only memory. Hence the crash.

You can fix your code by making 'str' an array instead:

   char str[] = "0.12000, 0.0, 0.0";

Then the string will be stored temporarily on the stack, which is writable. 
The array will be just the right size to accommodate the string.

When using GCC, I recommend you compile with -Wwrite-strings. String
constants 
will then be given the 'const' qualifier, and you will get a warning if you 
don't use 'const' yourself where necessary:

   const char *str = "0.12000, 0.0, 0.0";

Ben


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]