L
liljencrantz
Hi,
I have a piece of code that uses hashtables to store pointers to
various bits of data. The hashtable sees all pointers as const void *,
while the application obviously uses various other pointer types for
the data. I've run into a warning with the following code:
void hash_remove( hash_table_t *h,
const void *key,
const void **old_key,
const void **old_data );
void function_remove( const wchar_t *name )
{
void *key;
function_data_t *d;
event_t ev;
hash_remove( &function,
name,
(const void **) &key,
(const void **)&d );
....
}
Compiling it using GCC 4.1 results in the following warning:
gcc -g -O2 -std=c99 -fno-optimize-sibling-calls -Wall -D _GNU_SOURCE
-c -o function.o function.c function.c: In function
'function_remove':
function.c:204: warning: dereferencing type-punned pointer will break
strict-aliasing rules
The call to hash_remove is what is generating the warning.
My understanding about why this is a problem may be a bit blurry. In
general, using typecasting and pointer dereferencing does not seem to
be proper C99, and may end up breaking the compilers assumptions about
(non)aliasing of pointers of different types, and I suppose that is why
GCC is a bit unhappy here.
I can get around this by using a union of a const void * and the real
datatype, but this seems like a hacksih solution. Any pointers to the
proper way to do this type of typecasting, so as to avoid aliasing
problems would be welcome.
Also, I noticed that the comp.lang.c FAQ at
www.eskimo.com/~scs/c-faq.com/ returns an access error, and at least
the google cache of it contains no mention of aliasing. Perhaps a few
questions about aliasing could be added to the FAQ as well?
I have a piece of code that uses hashtables to store pointers to
various bits of data. The hashtable sees all pointers as const void *,
while the application obviously uses various other pointer types for
the data. I've run into a warning with the following code:
void hash_remove( hash_table_t *h,
const void *key,
const void **old_key,
const void **old_data );
void function_remove( const wchar_t *name )
{
void *key;
function_data_t *d;
event_t ev;
hash_remove( &function,
name,
(const void **) &key,
(const void **)&d );
....
}
Compiling it using GCC 4.1 results in the following warning:
gcc -g -O2 -std=c99 -fno-optimize-sibling-calls -Wall -D _GNU_SOURCE
-c -o function.o function.c function.c: In function
'function_remove':
function.c:204: warning: dereferencing type-punned pointer will break
strict-aliasing rules
The call to hash_remove is what is generating the warning.
My understanding about why this is a problem may be a bit blurry. In
general, using typecasting and pointer dereferencing does not seem to
be proper C99, and may end up breaking the compilers assumptions about
(non)aliasing of pointers of different types, and I suppose that is why
GCC is a bit unhappy here.
I can get around this by using a union of a const void * and the real
datatype, but this seems like a hacksih solution. Any pointers to the
proper way to do this type of typecasting, so as to avoid aliasing
problems would be welcome.
Also, I noticed that the comp.lang.c FAQ at
www.eskimo.com/~scs/c-faq.com/ returns an access error, and at least
the google cache of it contains no mention of aliasing. Perhaps a few
questions about aliasing could be added to the FAQ as well?