Questions about GCC 3.3 strict aliasing warning

Jonathan Lennox
Tue Dec 16 01:42:00 GMT 2003

Hello, all:

I've got existing code that defines an "object" API in C.  This API defines
opaque objects which are operated on by a library, but which also permit
user-specific data to accompany the objects.  These objects are accessed via
void* pointers passed by the library.

Here's a simplified version of the code:
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: objectref.c
URL: <>
-------------- next part --------------

Now, up through GCC 3.2.x, this code produced no warnings.  However, as of
GCC 3.3.x, the line
      err = GetUserContext(the_object, (void**)&uc);
produces the warning
objectref.c:31: warning: dereferencing type-punned pointer will break strict-aliasing rules

I have several questions:
1.  Is the function my_get_func() indeed non-conformant C?

2.  Might GCC actually mis-compile it?  (It seems like it would need
    inter-module optimization to do so, assuming GetUserContext isn't an
    inline function and that all object pointers have the same

3.  Is the function my_get_func2() conformant?  (It generates no -W -Wall
    warnings with GCC 3.2.2.)

4.  Is there any way to write this code conformantly without needing the
    intermediate void* variable?

5.  If not, will GCC (with -O2) always realize that the void* variable is
    unnecessary, and optimize it out?


I've included the output of gcc -v below.

$ gcc -v -Wall -g -O2 -c objectref.c
Reading specs from /home/lennox/Proj/New-GNU/lib/gcc-lib/sparc-sun-solaris2.8/3.3.2/specs
Configured with: /home/lennox/Proj/NoBackup/build/gcc-3.3.2/configure --prefix=/home/lennox/Proj/New-GNU --with-gnu-as --with-as=/usr/local/gnu/bin/as --with-gnu-ld --with-ld=/usr/local/gnu/bin/ld --enable-version-specific-runtime-libs
Thread model: posix
gcc version 3.3.2
 /home/lennox/Proj/New-GNU/lib/gcc-lib/sparc-sun-solaris2.8/3.3.2/cc1 -quiet -v -D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=2 -Dsparc -D__sparc__ -D__sparc -D__GCC_NEW_VARARGS__ -Acpu=sparc -Amachine=sparc objectref.c -quiet -dumpbase objectref.c -auxbase objectref -g -O2 -Wall -version -o /var/tmp//cc1j4kMI.s
GNU C version 3.3.2 (sparc-sun-solaris2.8)
        compiled by GNU C version 3.3.2.
GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32768
ignoring nonexistent directory "/home/lennox/Proj/New-GNU/sparc-sun-solaris2.8/include"
#include "..." search starts here:
#include <...> search starts here:
End of search list.
objectref.c: In function `my_get_func':
objectref.c:32: warning: dereferencing type-punned pointer will break strict-aliasing rules
 /usr/local/gnu/bin/as -V -Qy -s -o objectref.o /var/tmp//cc1j4kMI.s
GNU assembler version (sparc-sun-solaris2.8) using BFD version

Jonathan Lennox
lennox at cs dot columbia dot edu

More information about the Gcc-help mailing list