offset관련 질문
  1 #include 
  2 
  3 #define show(type, member) \
  4     (unsigned long)(&((type *)0)->member)
  5 
  6 struct s_a {
  7     char a;
  8     char b;
  9     int c;
 10     char d;
 11     char e;
 12 }test;
 13 
 14 void main()
 15 {
 16     int result[5];
 17 
 18     result[0] = show(struct s_a, a);
 19     result[1] = show(struct s_a, b);
 20     result[2] = show(struct s_a, c);
 21     result[3] = show(struct s_a, d);
 22     result[4] = show(struct s_a, e);
 23 
 24     int i=0;
 25     for(i=0; i<5; i++)
 26         printf("The result[%d] is..%d\n", i, result[i]);
 27 }
 28 /*
 29 The result[0] is..0
 30 The result[1] is..1
 31 The result[2] is..4
 32 The result[3] is..8
 33 The result[4] is..9
 34  */
======================================================================================
제일 아래 주석이 결과입니다.
char와 int의 크기만 고려한다면..
 29 The result[0] is..0
 30 The result[1] is..1
 31 The result[2] is..2
 32 The result[3] is..6
 33 The result[4] is..7
이런 결과가 나와야 할 것 같은데요..
b와 c사이에 공간(2)은 왜 생긴거죠??
a,b,c,d,e를 같은 자료형으로만 하면 이런 공간이 안생기던데요...
컴파일러에 따라 다른 특징이 있는건가요?
우분투 gcc입니다.


structure alignment에 관련된 사항입니다.
다음 사이트를 참조하시길...
http://en.wikipedia.org/wiki/Data_structure_alignment
www.gilgil.net
감사합니다.^^
덕분에 궁금증을 해결했습니다.
좋은하루 되세요.^^
댓글 달기