public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/35384]  New: Variables declared as 'static char * avar = "some string";' cannot be modified
@ 2008-02-26 18:57 Quinlan at ACM dot org
  2008-02-26 19:00 ` [Bug c/35384] " Quinlan at ACM dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Quinlan at ACM dot org @ 2008-02-26 18:57 UTC (permalink / raw)
  To: gcc-bugs

A variable is declared similar to this:
  static char * avar = "some string";
Then an attempt to change the value like this will throw a SIGSEGV:
  avar[4] = '_';
The problem occurs when the variable is declared locally in a function, and
when the variable is declared globally. Adding a cast to the assignment (as
suggested in another bug solution I found during my search) does not alter the
behavior:
  avar[4] = (unsigned char)'_';

Changing the way the variable is declared does work around the problem:
  static char * avar = NULL; // Local or global
   ...
  if(avar == NULL) avar = "some string"; // In a function
   ...
  avar[4] = '_'; // Now this will work

I am seeing this problem using Linux Kernel 2.6.21.5 and gcc version 4.1.2.
I am using the gcc build from the Slackware 12.0 distribution.


-- 
           Summary: Variables declared as 'static char * avar = "some
                    string";' cannot be modified
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Quinlan at ACM dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35384


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-02-26 21:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-26 18:57 [Bug c/35384] New: Variables declared as 'static char * avar = "some string";' cannot be modified Quinlan at ACM dot org
2008-02-26 19:00 ` [Bug c/35384] " Quinlan at ACM dot org
2008-02-26 19:00 ` pinskia at gcc dot gnu dot org
2008-02-26 20:10 ` Quinlan at ACM dot org
2008-02-26 21:06 ` Quinlan at ACM dot org
2008-02-26 21:10 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).