IT

struct test{

   int a;

   char b;

   // short c;

   // char d;

   // char e;

   // short f;

};


int main()

{

  printf("%d\n",sizeof(struct test));


  return 0;

}


// 8byte 출력

// 구조체에 short c 추가 했을 경우에도 8byte

// 다시 구조체에 char d 추가 햇을 경우에는 12byte


a(int)

b(char)

 

c(short)

d(char)

e(char)

f(short)

 4+4+4 = 12byte

32bit 컴파일러 (4byte)이기 때문에 4byte를 기준으로 최적화 하기 때문에

위의 도표와 같은 메모리 공간에 위치하게 된다.


a(int)

b(char)

d(char)

c(short)

e(char)

f(short)

 

4+4+1+2 = 11byte

/*

#pragma pack (1) // 1byte로 컴파일 최적화

#pragma pack (4) // 4byte로 컴파일 최적화

*/


#include <stdio.h>


#pragma pack (1)

struct test {

        int a;

        char b;

        //char d;

        //short c;

        //char d;

        //char e;

        //short f;

};

#pragma pack(4)


int main()

{

        printf("%d\n",sizeof(struct test)); 

        return 0;

}

'소프트웨어 > C언어' 카테고리의 다른 글

Heap, Stack 구조  (0) 2011.07.18
volatile  (0) 2011.06.30
Return  (0) 2011.06.21
구조체 멤버변수 접근방법  (0) 2011.06.21
int main(int inum, char *str[])  (0) 2011.06.20
Posted by sinpk