This is the mail archive of the gcc@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]

Re: string variables and literals


On Wed, Aug 30, 2000 at 09:33:06PM -0700, sidster wrote:
> 
> Greetings,
> 
> * Alexandre Oliva (aoliva@redhat.com) [20000830 21:05]:
> > On Aug 31, 2000, Eric Lemings <elemings@uswest.net> wrote:
> > 
> > > Just as s3 was initialized with a copy of its string literal,
> > > shouldn't s4 be intialized with the location of a copy of the string
> > > literal placed in some arbitrary part of writable memory?
> > 
> > No, it shouldn't.  But it might, if someone contributes a patch to
> > assign strings stored in pointers to non-const char to writable
> > memory.  I don't know how hard it would be to do it.
> 
> 
> I may be missing something (mis-understanding this discussion or
> something) but what is the utility of such a patch?
> 
> 
> When you have code like:
> 
>    char* p = "some string";
> 
> 'p' is a pointer to a constant static string.  The memory location
> pointed to by 'p' should not be modifiable.
> 
> The compiler doesn't warn about this but the user should be smart enough
> to realize s/he can't modify the character array pointed to by 'p'.

You are entirely correct.  In fact you can get the compiler to warn
about this by specifying -Wwrite-strings on the command line.  Also,
in C++ the compiler always warns you.

zw


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