c++에서 구조체 사용할때..

gyunn의 이미지

#include
using namespace std;

struct student {
char id[20]; //20byte
char name[10]; //10byte
char dept[12]; //12byte
char gender[2]; //2byte
double grade; //8byte
int n; //4byte
};

void main() {
struct student b;
int a = 1;

cout << sizeof(b) << endl;;
cout << sizeof(int) << endl;;
cout << sizeof(a) << endl;
}
위의 구조체 b 크기를 출력하면 계속 64비트가 나옵니다.
저기서 int n 변수를 빼고 출력하면 56비트가 나오구요..
그래서 제 컴퓨터 64비트라 8비트로 int를 사용하나보다 하구
자료형 int 크기, int a 크기 를 출력했더니 4가 나옵니다.
어찌된 영문인가요?

gilgil의 이미지

gilgil의 이미지

구조체 선언 앞뒤로 pragma pack을 선언해 주면 structure alignment 크기를 임의로 설정해 줄 수도 있습니다.

#pragma pack(push, 1)
struct student {
char id[20]; //20byte
char name[10]; //10byte
char dept[12]; //12byte
char gender[2]; //2byte
double grade; //8byte
int n; //4byte
};
#pragma pack(pop)