#include <stdio.h>
struct node {
int i;
struct node *next;
};
struct node x;
main()
{
printf("%d\n",sizeof(x.i)); //결과 4
printf("%d\n",sizeof(x.next)); //결과 8(컴파일러 종류에 따라 4로나오는것도있지만 제 기준으로 8)
printf("%d\n",sizeof(x)); // 결과 16
printf("%d\n",sizeof(struct node)); // 결과 16
}
왜 맨 마지막 두개의 printf 결과가 16이 되는지 잘 모르겠습니다.
구조체는 연속된 메모리 공간을 할당하니까 4+8 = 12로 나와야하는거 아닌가요?
효율때문에 패딩됌. 가장 큰 8 * 2 = 16 ㅇㅇ
저도 질문좀요. 그러면 4byte단위로 구조체 인식하는 것은 4+8 =12 맞나요?
왜냐하면 32bit 컴퓨터는 4byte로 끊어서 인식하잖아요.