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: Why does this happen ??


>>>>> Koundinya K writes:

 > Hi,
 > 	While I have been trying to compile a large chunk of code I 
 > have the following piece of code ( to reverse a string - which I 
 > have simulated here to highlight the problem)

 > #include <stdio.h>
 > #include <string.h>

 > main()
 > {
 >         char    *s="Hello World";
 >         int     c, i, j;
 >         for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
 >                 c = s[i];
 >                 s[i] = s[j];
 >                 s[j] = (char)c;
 >         }
 >         fprintf(stdout,"Reversed string=%s\n",s);
 > }

 > When I compile this code with gcc, the resulting binary seg 
 > faults. This happens on 2 flavors of O.S that I tried ( My MIPS 
 > based SVR4.2, Redhat Linux)
 > ..

 > But when I compile the above code on using my native compiler, 
 > the resulting binary works.

 > Any explanation for this ?. Why does gcc behave like this ?.

Use -fwritable-strings, "Hello World" lives in read-only memory.

Andreas
-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.inka.de
    http://www.suse.de/~aj

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