Questions about GCC 3.3 strict aliasing warning

Falk Hueffner falk.hueffner@student.uni-tuebingen.de
Tue Dec 16 10:08:00 GMT 2003


Jonathan Lennox <lennox@cs.columbia.edu> writes:

> void my_get_func(object* the_object)
> {
>   my_user_context* uc;
>   errcode err;
> 
>   /* Breaks strict aliasing rules? */
>   err = GetUserContext(the_object, (void**)&uc);
> }
> 
> 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?

No. However, GetUserContext is if it actually dereferences its
argument. In other words, the warning is correct.

> 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
>     representation.)

Correct. However, future gccs might have inter-module optimizations.

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

Looks OK to me.

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

Have it return the void*, and use NULL to indicate an error.

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

Very likely yes. You could check the assembly to verify.

-- 
	Falk



More information about the Gcc-help mailing list