Does C guarantee that sizeof(long) == sizeof(void*)
? If not, why is it used so extensively in Linux kernel source code?
I looked at sizeof (int) == sizeof (void*)? but that talks about sizeof(int)
vs sizeof(void *)
.
Does C guarantee that sizeof(long) == sizeof(void*)
? If not, why is it used so extensively in Linux kernel source code?
I looked at sizeof (int) == sizeof (void*)? but that talks about sizeof(int)
vs sizeof(void *)
.
No, the C standard does not guarantee that sizeof(long) == sizeof(void *)
.
In practice, on Windows 64-bit systems, the values are 4
for sizeof(long)
and 8
for sizeof(void *)
. This design conforms to the C standard. See also What is the bit-size of long
on 64-bit Windows?
Those implementing the Linux kernel have presumably decided that they'll never port the code to a system that follows the Windows 64-bit LLP64 (long long
and pointers are 64-bit quantities) system, and therefore don't need to concern themselves with whether the sizes are different. Both the 32-bit systems (ILP32) and the 64-bit systems (LP64) do have sizeof(long) == sizeof(void *)
. But the C standard does not guarantee it.
The only guarantees are:
void *
and char *
have the same size and alignment;sizeof (const int *) == sizeof (int *)
);struct
pointer types have the same size and alignment;union
pointer types have the same size and alignment;That's it.
If Linux kernel developers are writing code that assumes sizeof (long) == sizeof (void *)
, then they've decided to limit which platforms they're going to support. Which is absolutely fine - you don't have to support every oddball architecture out there.