I'm playing around with pointers and I've noticed one weird thing.
I have a struct as follow:
typedef struct list_element_struct {
uint32_t x;
uint32_t y;
uint32_t z;
struct list_element_struct *next;
}list_element;
As far as I know, the size of unsigned int is 4 bytes, and the size of a pointer is 8 bytes. Also there are 8 bytes aligned happened here so the size of this struct is 24 bytes.
I've initialized a list of above struct objects with list_element els[5];
and also set every piece of data in there to 0 with memset(els,0,5*sizeof(list_element));
Right now I'm trying to see their memory locations with these piece of code:
printf("%p start location of els
", &(els));
printf("%p start location of els->x
", &(els->x));
printf("%p start location of els->y
", &(els->y));
printf("%p start location of els->z
", &(els->z));
printf("%p start location of els->next(pointer)
", &(els->next));
printf("%p start location of els+1
", &(els[1]));
What I have as printed out is:
0x7ffeeba4a970 start location of els
0x7ffeeba4a970 start location of els->x
0x7ffeeba4a974 start location of els->y
0x7ffeeba4a978 start location of els->z
0x7ffeeba4a980 start location of els->next(pointer)
0x7ffeeba4a988 start location of els+1
Here is the weird thing. Why does els->z only takes two bytes? it really should take 4 like x and y.
question from:
https://stackoverflow.com/questions/65848591/c-how-could-an-unsigned-int-only-takes-two-bytes 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…