Assume I have the following C code
#include <stdio.h>
int main(){
int a = 5;
int b = 6;
int c = 7;
void *d = &a;
long *ld = (long *)d;
printf("%ld\n", *ld);
return 0;
}
Doing a sizeof
using gdb
, I can see that an int
is 4 bytes and a long
is 8 bytes on my little endian, x64 machine.
The output of the above printf
is 5
.
My question is, how is the value 5 despite the fact that my ld
pointer is now 64 bits and pointing to the first byte of a
(a 32 bit varable). How is it not overflowing when i'm dereferencing it? How is it not including the bytes in b
and c
? How does it know to stop counting after 32 bits although it's a 64 bit pointer?