2012年4月16日 星期一
2012年4月3日 星期二
word alignment(pragma pack) --- 影響struct size的因素
struct syskeyd2
{
int a;
int b;
int c;
int d;
};
/*因為預設是32 bits定址,所以char c;佔4bytes, char d;也佔4bytes*/
struct syskeyd1
{
int a;
char c;
int b;
char d;
int e;
};
/*如果我把char c;跟char d;放在一起,那這c跟d就會一起進到4bytes裡,
結果sizeof(struct syskeyd1)會是16 bytes*/
/*以下做完pack()後, struct就會以1個個bytes去計算size跟參數定位*/
#if defined(_MSC_VER)
#define HANDLE_PRAGMA_PACK_PUSH_POP //WIN32 style pack
#elif defined(__BORLANDC__)
#define HANDLE_PRAGMA_PACK_PUSH_POP //WIN32 style pack
#else
#endif
#ifdef HANDLE_PRAGMA_PACK_PUSH_POP
#pragma pack(push,1)
#else
#pragma pack(1)
#endif
struct syskeyd3
{
int a;
char c;
int b;
char d;
int e;
};
#ifdef HANDLE_PRAGMA_PACK_PUSH_POP
#pragma pack(pop)
#else
#pragma pack()
#endif
/*****************************************************************************/
/* NAME : main() */
/*****************************************************************************/
main(int argc, char *argv[])
{
printf("syskeyd2[%d] syskeyd1[%d] syskeyd3[%d] \n",sizeof(struct syskeyd2), sizeof(struct syskeyd1),sizeof(struct syskeyd3));
return 0;
}
執行結果:
syskeyd2[16] syskeyd1[20] syskeyd3[14]
{
int a;
int b;
int c;
int d;
};
/*因為預設是32 bits定址,所以char c;佔4bytes, char d;也佔4bytes*/
struct syskeyd1
{
int a;
char c;
int b;
char d;
int e;
};
/*如果我把char c;跟char d;放在一起,那這c跟d就會一起進到4bytes裡,
結果sizeof(struct syskeyd1)會是16 bytes*/
/*以下做完pack()後, struct就會以1個個bytes去計算size跟參數定位*/
#if defined(_MSC_VER)
#define HANDLE_PRAGMA_PACK_PUSH_POP //WIN32 style pack
#elif defined(__BORLANDC__)
#define HANDLE_PRAGMA_PACK_PUSH_POP //WIN32 style pack
#else
#endif
#ifdef HANDLE_PRAGMA_PACK_PUSH_POP
#pragma pack(push,1)
#else
#pragma pack(1)
#endif
struct syskeyd3
{
int a;
char c;
int b;
char d;
int e;
};
#ifdef HANDLE_PRAGMA_PACK_PUSH_POP
#pragma pack(pop)
#else
#pragma pack()
#endif
/*****************************************************************************/
/* NAME : main() */
/*****************************************************************************/
main(int argc, char *argv[])
{
printf("syskeyd2[%d] syskeyd1[%d] syskeyd3[%d] \n",sizeof(struct syskeyd2), sizeof(struct syskeyd1),sizeof(struct syskeyd3));
return 0;
}
執行結果:
syskeyd2[16] syskeyd1[20] syskeyd3[14]
訂閱:
文章 (Atom)
文章分類
- 爬山 (3)
- 參考文章 (3)
- 鳥事 (5)
- 報稅 (1)
- AIX (2)
- ajax (1)
- BCB (3)
- C/C++ (2)
- cloudera (3)
- DISK (1)
- ftp (1)
- Fuse (2)
- gdb (2)
- hadoop (13)
- hdfs (8)
- HPC (2)
- hypertable (12)
- iOS (1)
- iscsi (1)
- JAVA (2)
- KFS (5)
- kickstart (1)
- KVM (2)
- LAMP (2)
- linux (2)
- Lion (1)
- LVM (2)
- mapreduce (3)
- mpi (3)
- mpich2 (4)
- msgpack (2)
- mysql (2)
- nfs (1)
- openmp (2)
- OS (1)
- OSX (2)
- others (5)
- PBS (1)
- performance_tuning (3)
- php (3)
- phplist (3)
- programming (27)
- REST (2)
- RHCA (6)
- rhel (13)
- rhel6 (4)
- scp (1)
- shell_scripts (2)
- snowleopard (2)
- Solaris (6)
- ssh (1)
- syslog (1)
- t-442-1 (4)
- torque (1)
- ubuntu (2)
- VNC (1)
- watercolor (5)
- windows (1)
- yum (1)