gcc-4.2.2 -O2 bug??
CH Gowri Kumar - గౌరీ కుమార్
gkumar007@gmail.com
Wed Jun 18 06:57:00 GMT 2008
Hi all,
The following program with gcc-4.2.2 is giving different results for
optimized and debug flags
$ gcc -Wall -O2 bug.c
$ ./a.out
0xAD
$gcc -Wall bug.c
$ ./a.out
0x0
<snip>
#include <stdio.h>
#include <stdlib.h>
typedef struct Val
{
unsigned long a[100];
}Val;
typedef struct table {
unsigned long a[100];
Val* val;
}Table;
unsigned int
subopt_hash(void *key)
{
Table* t = (Table *) key;
Val *tempVal;
unsigned char *k;
unsigned int h;
int i, size;
tempVal = t->val;
t->val = NULL;
k = (unsigned char *) t;
h = 0;
size = sizeof(Table);
for(i=0; i<size;i++) {
h += *(k+i);
}
t->val = tempVal;
return (h);
}
int main()
{
Table* t;
unsigned int ret;
t = calloc(1,sizeof(Table));
t->val = (Val*) 0XAD;
ret = subopt_hash(t);
printf("0x%X\n",ret);
return 0;
}
</snip>
The statement t->val =NULL in subopt_hash seems to be optimized away.
Though it is not directly used, the resultant value does depend on it
being NULL.
There are no warnings reported either for it.
Version of gcc:
============
gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../../src/gcc-4.2.2/configure
--prefix=/depot/gcc-4.2.2-static --disable-shared
--enable-threads=posix --disable-checking --with-system-zlib
--enable-__cxa_atexit --disable-libunwind-exceptions
--enable-languages=c,c++,objc,fortran --with-cpu=generic
--host=i386-redhat-linux
Thread model: posix
gcc version 4.2.2
Is this a bug in gcc?
or
Do you suggest any other flags to get warnings?
(I have -Wstrict-aliasing, but didn't get any warnings)
Regards,
Gowri Kumar
More information about the Gcc-help
mailing list