This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: strict aliasing question


On Fri, Nov 10, 2006 at 02:32:10PM -0800, Howard Chu wrote:
> 
> With the previous example, if alias1.c was instead:
> 
> ####
> #include <stdio.h>
> 
> extern void getit( void **arg );
> 
> main() {
>        union {
>                int *foo;
>                void *bar;
>        } u;
> 
>        getit( &u.bar );
>        printf("foo: %x\n", *u.foo);
> }
> ####
> 
> gcc no longer complains, but according to the spec, the code is not any 
> more reliable.

   As far as I know, memcpy() is the answer:

#include <stdio.h>
#include <string.h>

extern void getit (void **arg);

int main ()
{
	int *foo;
	void *bar;

	getit (&bar);
	memcpy (&foo, &bar, sizeof (foo));
	printf ("foo: %x\n", *foo);
	return (0);
}

-- 
Rask Ingemann Lambertsen


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]