C ÓïÑÔÊý¾Ý¶ÔÆë
#include <stdio.h>
struct Foo1
{
char a;
int b;
char c;
int d;
};
#pragma pack (2)
struct Foo2
{
char a;
int b;
char c;
int d;
};
#pragma pack ()
struct Foo3
{
char a;
char c;
int b;
int d;
};
struct Foo4
{
char a;
int b;
char c;
int d;
} __attribute__ ((__packed__));
int main(int argc, char **argv)
{
printf("size of Foo1: %d ", sizeof(struct Foo1));
printf("size of Foo2: %d ", sizeof(struct Foo2));
printf("size of Foo3: %d ", sizeof(struct Foo3));
printf("size of Foo4: %d ", sizeof(struct Foo4));
return 0;
}
ÏÂÃæµÄС³ÌÐòÊǹØÓÚÊý¾Ý¶ÔÆëµÄÒ»¸öÊÔÑ飬ÔÚÎÒµÄ 2.4 ÄÚºËµÄ Linux ÉÏÔËÐнá¹ûÈçÏ£º
size of Foo1: 16
size of Foo2: 12
size of Foo3: 12
size of Foo4: 10
1¡¢ Foo1 δ¾¹ýÈκδ¦ÀíµÄ£¬ÓÉÓÚÕûÐÍÐèÒª°´ 4 ×Ö½Ú¶ÔÆ룬Òò´Ëʵ¼Ê´æ´¢½á¹ûÈçÏ£¬Õ¼Óà 16 ¸ö×Ö½Ú¿Õ¼ä
2¡¢Foo2 ͨ¹ý #pragma pack (2) £¬½«¶ÔÆ뷽ʽ¸ÄΪ 2 ×Ö½Ú¶ÔÆ룬ʵ¼Ê´æ´¢½á¹ûÈçÏ£¬Õ¼Óà 12 ×Ö½Ú¿Õ¼ä
3¡¢ Foo3, ͨ¹ý #pragma pack ()£¬ÓÖ»Ö¸´µ½Ä¬È쵀 4 ×Ö½Ú¶ÔÆ뷽ʽ£¬µ«ÊÇÓÉÓÚµ÷ÕûÁ˱äÁ¿µÄ˳Ðò£¬Êµ¼Ê´æ´¢½á¹ûÈçÏ£¬Õ¼Óà 12 ×Ö½Ú¿Õ¼ä
4¡¢Foo4, ͨ¹ý GCC µÄ __packed__ µÄÀ©Õ¹ÊôÐÔ£¬½ûÖ¹¶ÔÆ룬µÃµ½ÈçϽá¹û£¬Õ¼Óÿռä 10 ×Ö½Ú
Ïà¹ØÎĵµ£º
תÌû£ºÒ»»ÔµÄÎÄÕÂ
ÔÚLinuxÏÂ×ö¿ª·¢ÄÑÃâÒª½Ó´¥makefile£¬Õû¸öÏîÄ¿µÄ¹¹½¨¶¼ÒÀÀµÓÚËü¡£100¸ödeveloperÓÐ100ÖÖmakefileµÄд·¨£¬ÔÚÒ»¸ö½Ï´óµÄÏîÄ¿ÖУ¬¸÷ÖÖ¸÷ÑùµÄmakefileÎÞÂÛÔÚ¿ª·¢¡¢ºóÆÚά»¤»¹ÊÇÕû¸öϵͳµÄ³ÖÐø¼¯³É¶¼ÊÇÒ»¸ö¸ºµ£¡£
ÓÐÐÒ²ÎÓëÖع¹Ò»¸öÒÅÁôϵͳµÄmakefile£¬ÒÔÏÂÊÇһЩÐĵúÍÒ»¸ömakefileÄ£°å¡£
Öع¹Ä¿µÄ£º
1.Çå ......
ÏÂÃæµÄÀý×Ó¶¼ÔÚUbuntu8.04 GCCϱàÒëµÄ½á¹û£¬ÓÐЩûÓиø½á¹û
±à³ÌÒ»¶¨Òª×Ô¼º¶¯ÊÖÊÔÒ»ÊÔ£¡
1.¶¨ÒåÓëÉùÃ÷£¬¶¨ÒåÒª·ÖÅäÄڴ棬ÉùÃ÷Ö»ÊÇÉùÃ÷Ôڱ𴦶¨ÒåÁË
int a; //¶¨Òå
extern int a; //ÉùÃ÷
char str[100]
extern char str[] //ok
char * str[]
extern char str[] //error
char ......
1.ÉèÓÐÒÔÏÂ˵Ã÷ºÍ¶¨Ò壺
typedef union {long i; int k[5]; char c;} DATE;
struct data { int cat; DATE cow; double dog;} too;
DATE max;
ÔòÓï¾ä printf("%d",sizeof(struct date)+sizeof(max));µÄÖ´Ðнá¹ûÊÇ£º £¿£¿£¿
´ð£ºDATEÊÇÒ»¸öunion, ±äÁ¿¹«Óÿռä. ÀïÃæ×î´óµÄ±äÁ¿ÀàÐÍÊÇint[5], Õ¼ÓÃ20¸ö×Ö½Ú. ËùÒÔËüµÄ´óС ......
¡¾³ÌÐò1¡¿
ÌâÄ¿£ºÓÐ1¡¢2¡¢3¡¢4¸öÊý×Ö£¬ÄÜ×é³É¶àÉÙ¸ö»¥²»ÏàͬÇÒÎÞÖظ´Êý×ÖµÄÈýλÊý£¿¶¼ÊǶàÉÙ
£¿
1.³ÌÐò·ÖÎö£º¿ÉÌîÔÚ°Ùλ¡¢Ê®Î»¡¢¸öλµÄÊý×Ö¶¼ÊÇ1¡¢2¡¢3¡¢4¡£×é³ÉËùÓеÄÅÅÁкóÔÙÈ¥
¡¡¡¡¡¡¡¡¡¡¡¡µô²»Âú×ãÌõ¼þµÄÅÅÁС£
2.³ÌÐòÔ´´úÂ룺
main()
{
int i,j,k;
printf("\n");
for(i=1;i<5;i++)¡¡¡¡¡¡¡¡£¯*ÒÔÏ ......
Ò»£¬²»Í¬¹Ø¼ü×Ö£¬ÏµÍ³Ñ¹Õ»·½Ê½
1£¬Èç¹ûº¯ÊýfuncÊÇ__cdecl(VCϵÄĬÈϵ÷Ó÷½Ê½)£¬µ÷ÓÃʱÇé¿öÈçÏÂ
int main()
{
//²ÎÊý´ÓÓÒµ½×óѹջ
push 4
push 3
push 2
push 1
call func
add esp 0x10 //µ÷ÓÃÕ߻ָ´¶ÑÕ»Ö¸Õëesp£¬4¸ö²ÎÊýµÄ´óСÊÇ0x10(4x4)
}
Cµ÷ÓÃÔ¼¶¨£¨¼´ÓÃ__cdecl¹Ø¼ü×Ö˵Ã÷£©°´´ÓÓÒÖÁ×óµÄ˳Ðòѹ² ......