On Thu, Jan 07, 2010 at 10:09:23AM +0100, Stefan Reinauer wrote:
On 1/7/10 5:23 AM, Kevin O'Connor wrote:
I don't think the above is a problem (the pointer foo resides on the stack, but the string contents do not). The following is a problem:
const char foo[]="foobar\n";
(both the pointer and the array contents are on the stack), and indeed this does generate a warning from gcc.
Not with my default 4.3.2 -Wall -Wextra
That's odd. I used:
----------------------------------------------- #include <stdio.h> const char *func_foo(void) { const char foo[]="foobar\n"; return foo; } int main(void) { const char *foo;
foo = func_foo(); printf("%s\n", foo); return 0; } -----------------------------------------------
$ gcc --version gcc (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20) $ gcc -O -Wall constonstack.c constonstack.c: In function ‘func_foo’: constonstack.c:5: warning: function returns address of local variable
$ gcc34 --version gcc34 (GCC) 3.4.6 20060404 (Red Hat 3.4.6-18) $ gcc34 -O -Wall constonstack.c constonstack.c: In function `func_foo': constonstack.c:5: warning: function returns address of local variable
-Kevin