간단한 구조체의 크기를 sizeof 연산자를 통해 계산해 보자.
데이터형의 크기를 알려면 sizeof 연산자를 쓰면 된다.
쓸데없는(?) 구조체를 만들고, 데이터형의 크기를 맞춰보자.
컴파일 하기전에 손파일링해보자. 꼭
#include <stdio.h>
typedef struct _bts_t {
int a; // 4
int b; // 4 // 4+4=8 (o)
} bts_t;
typedef struct _exo_t {
char a; // 1
int b; // 4 // 1+4=5 (x), 1+3+4=8 (o)
} exo_t;
typedef struct _ses_t {
char a; // 1
char b; // 1
char c; // 1
char d; // 1
char e; // 1
int f; // 4 // 5+4=9 (x) 12(o)
} ses_t;
typedef struct _hot_t {
int a; // 4
int b; // 4
double c; // 8
int d; // 4 // 20 (x) 24(o)
} hot_t;
int main() {
printf("bts_t size is %zu\\r\\n", sizeof(bts_t));
printf("exo_t size is %zu\\r\\n", sizeof(exo_t));
printf("ses_t size is %zu\\r\\n", sizeof(ses_t));
printf("hot_t size is %zu\\r\\n", sizeof(hot_t));
}
(1)은 이해가 가는데,
(2)는 어라? 이상하다. 5가 나와야 하는데 8이 나온다.
(2)만 이해하면 나머지는 모두 이해된다.