Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sizeof(int) guaranteed to equal sizeof(void*)

Tags:

c

sizeof

Is the size of the datatype "int" always equals to the size of a pointer in the c language?

I'm just curious.

like image 318
scravy Avatar asked Dec 03 '22 00:12

scravy


1 Answers

Not at all, there is no guarantee that sizeof(int) == sizeof(void*). And on Linux/AMD64 sizeof(int) is 4 bytes, and sizeof(void*) is 8 bytes (same as sizeof(long) on that platform).

Recent C standard (e.g. C99) defines a standard header <stdint.h> which should define, among others, an integral type intptr_t which is guaranteed to have the size of pointers (and probably even which is reversably castable to and from pointers).

I think that the standard does not guarantee that all pointers have the same size, in particular pointer to functions can be "bigger" than data pointers (I cannot name a platform where it is true). I believe that recent Posix standard requires that (e.g. for dlsym(3)).

See also this C reference and the n1570 draft C11 standard (or better)

PS. In 2021 I cannot name a common platform with sizeof(long) != sizeof(void*). But in the previous century the old intel 286 could have been such a platform.

like image 140
Basile Starynkevitch Avatar answered Dec 15 '22 17:12

Basile Starynkevitch