Modifying string literals (was Re: Are strings local?)

Leo de Wit leo at philmds.UUCP
Sat Aug 6 08:03:44 AEST 1988


In article <24986 at think.UUCP> barmar at kulla.think.com.UUCP (Barry Margolin) writes:
>In article <652 at m10ux.UUCP> rgr at m10ux.UUCP (Duke Robillard) writes:
>>During my search about this locality of statics business, I discovered
>>a pretty bizarre thing about strings.  I dunno if it's just my System
>>V compiler or what, but you can write to strings (since they're just
>>static arrays of characters?)
>
>Yes, compilers will allow this, but it is not a good idea, and I
>believe that ANSI C specifically says that it is a no-no.  A compiler
>is permitted to allocate string literals in read-only memory.  I think
   [rest deleted]...

Indeed. The VMS C compiler does just this.
I believe something like

   tmpnam("fooXXXXXX");

where tmpnam should fill in the XXXXXX with a (process unique?) id,
just stack dumped because it tried to write into readonly memory.
Not that I'm suggesting anyone should use VMS or VMS C ........ 8-).

     Leo.



More information about the Comp.lang.c mailing list