Is it possible to consider sizeof (double)> = sizeof (void *)?

Can we assume that sizeof(double) will always be greater than or equal to sizeof(void*) ?

To do this in some context, is the following portable?

 int x = 100; double tmp; union { double dbl; void* ptr; } conv; conv.ptr = (void*)&x; tmp = conv.dbl; conv.dbl = tmp; printf("%d\n", *((int*)conv.ptr)); 

It works on several machines on which I tested it, but I see that it is terribly wrong if sizeof(void*) > sizeof(double) .

+1
source share
2 answers

In existing systems, yes. double - 64 bits in all current and future systems, since they coincide with the arithmetic double precision of IEEE. It is unlikely, but certainly possible, that pointers may be larger in the future - probably not for the sake of a larger address space, but instead for transferring limited information with them.

In any case, it seems like a very bad idea to rely on any relationship between double and void * ...

+3
source

Size has nothing to do with it. Some bits will always be stored there, and the size will always be large enough to hold void* . What goes wrong is that you interpret an almost random bit pattern as a pointer, it can do nothing but crash, but most likely you already knew that. Do not do this.

+1
source

Source: https://habr.com/ru/post/1012018/


All Articles