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 ??



Or better yet, for future coding:   don't change the string literals if
you don't have to.... There's surely an optimization that will eliminate
duplicate string literals globally in the application.... so changing
one string literal may change it for everything...

It'd be better to change the line:
    char *s="Hello World";
to
    char s[]="Hello World";

this will make a stack array of 11+1 chars containing "Hello World"
which you are free to modify and are ensured that no where else is that
memory being used.



Alexandre Oliva wrote:

> On Nov 29, 2000, "Koundinya.K" <kk@ddeorg.soft.net> wrote:
>
> >         char    *s="Hello World";
>
> > When I compile this code with gcc, the resulting binary seg
> > faults.
>
> GCC places string literals in read-only sections, unless you compile
> with -fwritable-strings.  If you had written `static char s[]="..."',
> it wouldn't have crashed, because then s's storage would be the
> string's storage, and it would be placed in read&write memory.
>
> --
> Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
> Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
> CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
> Free Software Evangelist    *Please* write to mailing lists, not to me


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