This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Weird pointer declaration bug
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Weird pointer declaration bug
- From: Karl-Max Wagner <karlmax at oberland dot net>
- Date: Sun, 27 Aug 2000 22:17:40 GMT
Hi GCC maintainers,
I recently had weird behaviour in a medium C project and finally
found out that pointer variables declared in a function are not
correctly allocated memory space. Here is a simple program that
segfaults under egcs 1.1.2 and gcc-2.95.2:
bugtest.c:
void foo ( float bar ) {
float *d;
float *e;
*e = 1;
*d = 0;
}
int main ( void ) {
unsigned int *a, *b;
float c[5000];
*a = 2;
*b = 2;
foo ( 1 );
return 0;
}
This is compiled with gcc -g -o bugtest bugtest.c
gdb output:
Single stepping with gdb shows the following:
GNU gdb 4.18
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License,
and you are
welcome to change it and/or distribute copies of it under
certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty"
for details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) b main
Breakpoint 1 at 0x80483e5: file bugtest.c, line 14.
(gdb) r
Starting program:
/root/HurricaneFactory/Test_Apps/SSST/BUG/bugtest.2.95
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
Breakpoint 1, main () at bugtest.c:14
14 *a = 2;
(gdb) s
15 *b = 2;
(gdb) s
16 foo ( 1 );
(gdb) s
foo (bar=1) at bugtest.c:5
5 *e = 1;
(gdb) p d
$1 = (float *) 0x0
(gdb) p e
$2 = (float *) 0x0
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x80483c9 in foo (bar=1) at bugtest.c:5
5 *e = 1;
(gdb) s
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb)
It seems that none of the pointer variables declared in function
foo are assigned proper memory space - in both cases 0x0 is
shown as address. Subsequent attempt to assign values to them
result in a segfault.
This is not a gdb bug: the program segfaults when running it
directly, too.
This seems to be a compiler bug - I don't see anything illegal
about declaring and using pointer variables in a function.
The problem seems to go away under egcs 1.1.2 when the array c
is smaller ( a few hundred ). Under gcc-2.95 it seems even to
exists when the array has a size of 5. Anyway, it seems to get
worse when the array is made larger.
I append an archive with the sources, debuggings, assembler
output and binaries made using both egcs 1.1.2 and gcc-2.95.
My system is a PII/400 with 128 MB RAM ( typical average
machine ) using Slackware 7.0. For testing I also compiled the
gcc-2.95 according to the accompanying instructions. This worked
without a hitch.
What worries me is that my test program is so simple. I am an
average C programmer at best and I am still worrying that I made
some stupid mistake although I can't find one. I guess the
problem is well known and there is a workaround - although I
didn't find one on the GCC site. Please tell me what's going on
- even when it just means telling me that I made a stupid
mistake......
=======================================================================
"It was hell. They knew it. Karl-Max Wagner
But they called it karlmax@oberland.net
W-I-N-D-O-Z-E" ham radio: DB8CO
*********Member of No Code International*********
***********Visit http://www.nocode.org***********
********Membership Number NCI-2563-DB8CO*********
_
/ / (_)__ __ ____ __
/ /__/ / _ \/ // /\ \/ / . . . t h e c h o i c e o f a
/____/_/_//_/\_,_/ /_/\_\ G N U g e n e r a t i o n . .
"Et ceterum censeo ut Microsoftem delendum esse" (Cato, adapted)
=======================================================================
bug.tar.bzip2