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

Reply via email to