Problem with -O3 and int to pointer (64bits)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
the following code converts a class A which is know to be of the size
of an int into a void*. This allow to save it in a list of void*.
When compiled without -O3 (or 2) it works. But with -O3 or 2, on
x86_64, the line intarray[1] = 0 is removed in the assembly output and
it doesn't work anymore.
Changing function topointer() into this:
return (void *)  (unsigned long)(*(((unsigned int *)&a)));
fix the problem.

So, is it a bug in gcc or does this does something bad?
Also do you know a good way to convert an int to a pointer?

Thank you.
Ben

/*----- code follows ------*/
#include <stdio.h>

void **listA;

class A {
public:
       A(int n) { x = n; }
       int x;
};

void *topointer(A a)
{
       unsigned int intarray[2];
       intarray[0] = *(((unsigned int *)&a));
       intarray[1] = 0;
       void *hack = *(void **)intarray;
       return hack;
}

void put(int i, A a)
{
 listA[i] = topointer(a);
}

A get(int i)
{
       void *hack = listA[i];
       return (*(A *) &hack);
}

int main(int argc, char **argv)
{
       listA = new void*[2];
       put(0, A(42));
       put(1, A(0));
       printf("%d\n", get(0).x);
       printf("%d\n", get(1).x);
}
/*--- code ends ------- */

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux