This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: gcc 4.1.1: char *p = "str" puts "str" into rodata
- From: Denis Vlasenko <vda dot linux at googlemail dot com>
- To: Andrew Haley <aph at redhat dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Sun, 28 Jan 2007 18:24:11 +0100
- Subject: Re: gcc 4.1.1: char *p = "str" puts "str" into rodata
- References: <200701281657.07030.vda.linux@googlemail.com> <17852.52236.56353.401310@zebedee.pink>
On Sunday 28 January 2007 17:15, Andrew Haley wrote:
> [ off-topic for gcc; redirected to gcc-help ]
>
> Denis Vlasenko writes:
> > char p;
> > int main() {
> > p = "";
> > return 0;
> > }
>
> This is odd code: you're assigning a pointer to a string to a char
> variable.
Sorry, should be "char *p;"
>
> > Don't you think that "" should end up in rw data?
>
> No: it's a literal string. Many, many years ago, C compilers put
> literal strings into read/write memory and it was possible to alter
> them, but C89 outlawed that practice.
gcc doesn't warn me, it just produces buggy code.
I remember that string literals are special - they decay to "const char *" OR
to "char*" depending on context. In this context, it should decay to "char*",
and it does - gcc doesn't complain "assingment of const to non-const", the bug
is that gcc placed "str" in ro section.
I did get SEGV on this, in busybox project.
--
vda