C/C++ÖеĽṹÌå¶ÔÆëÎÊÌâ(ÄÚ´æ¶ÔÆë)
ÓÉÓÚ³ÌÐòÔËÐÐʱռÓõÄÄÚ´æ¹ý´ó£¬ËùÒÔÏë°ì·¨¸ø³ÌÐòÊÝÉí¡£
ÔÚµ÷ÊÔÖз¢ÏֽṹÌåÕ¼ÓõÄsize¾¹È»ºÍÔ¤ÏëµÄ²»Ò»Ñù£¬ÔÀ´……
¿´¿´ÏÂÃæ½²µÄ°É£¬¿Ï¶¨»á²»Í÷´Ë¿´Å¶£¡
1,±ÈÈç:
struct{
short a1;
short a2;
short a3;
}A;
struct{
long a1;
short a2;
}B;
sizeof(A)=6, sizeof(B)=8,Ϊʲô£¿
×¢£ºsizeof(short)=2,sizeof(long)=4
ÒòΪ:“³ÉÔ±¶ÔÆëÓÐÒ»¸öÖØÒªµÄÌõ¼þ,¼´Ã¿¸ö³ÉÔ±°´×Ô¼ºµÄ·½Ê½¶ÔÆë.Æä¶ÔÆëµÄ¹æÔòÊÇ,ÿ¸ö³ÉÔ±°´ÆäÀàÐ͵ĶÔÆë²ÎÊý(ͨ³£ÊÇÕâ¸öÀàÐ͵ĴóС)ºÍÖ¸¶¨¶ÔÆë²ÎÊý(ÕâÀïĬÈÏÊÇ8×Ö½Ú)ÖнÏСµÄÒ»¸ö¶ÔÆë.²¢ÇҽṹµÄ³¤¶È±ØÐëΪËùÓùýµÄËùÓжÔÆë²ÎÊýµÄÕûÊý±¶,²»¹»¾Í²¹¿Õ×Ö½Ú.”(ÒýÓÃ)
½á¹¹ÌåAÖÐÓÐ3¸öshortÀàÐͱäÁ¿£¬¸÷×ÔÒÔ2×Ö½Ú¶ÔÆ룬½á¹¹Ìå¶ÔÆë²ÎÊý°´Ä¬ÈϵÄ8×Ö½Ú¶ÔÆ룬Ôòa1,a2,a3¶¼È¡2×Ö½Ú¶ÔÆ룬Ôòsizeof(A)Ϊ6£¬ÆäÒ²ÊÇ2µÄÕûÊý±¶£»
BÖÐa1Ϊ4×Ö½Ú¶ÔÆ룬a2Ϊ2×Ö½Ú¶ÔÆ룬½á¹¹ÌåĬÈ϶ÔÆë²ÎÊýΪ8£¬Ôòa1È¡4×Ö½Ú¶ÔÆ룬a2È¡2×Ö½Ú¶ÔÆ룬½á¹¹Ìå´óС6×Ö½Ú£¬6²»Îª4µÄÕûÊý±¶£¬²¹¿Õ×Ö½Ú£¬Ôöµ½8ʱ£¬·ûºÏËùÓÐÌõ¼þ£¬Ôòsizeof(B)Ϊ8£»
¿ÉÒÔÉèÖóɶÔÆëµÄ
#pragma pack(1)
#pragma pack(push)
#pragma pack(1)
struct{
short a1;
short a2;
short a3;
}A;
struct{
long a1;
short a2;
}B;
#pragma pack(pop)
½á¹ûΪsizeof( A)=6,sizeof( B)=6
************************
2,ÓÖÈç:
#pragma pack(8)
struct S1{
char a;
long b;
};
struct S2 {
char c;
struct S1 d;
long long e;
};
#pragma pack()
sizeof(S2)½á¹ûΪ24.
³ÉÔ±¶ÔÆëÓÐÒ»¸öÖØÒªµÄÌõ¼þ,¼´Ã¿¸ö³ÉÔ±·Ö±ð¶ÔÆë.¼´Ã¿¸ö³ÉÔ±°´×Ô¼ºµÄ·½Ê½¶ÔÆë.
Ò²¾ÍÊÇ˵ÉÏÃæËäȻָ¶¨ÁË°´8×Ö½Ú¶ÔÆë,µ«²¢²»ÊÇËùÓеijÉÔ±¶¼ÊÇÒÔ8×Ö½Ú¶ÔÆë.Æä¶ÔÆëµÄ¹æÔòÊÇ,ÿ¸ö³ÉÔ±°´ÆäÀàÐ͵ĶÔÆë²ÎÊý(ͨ³£ÊÇÕâ¸öÀàÐ͵ĴóС)ºÍÖ¸¶¨¶ÔÆë²ÎÊý(ÕâÀïÊÇ8×Ö½Ú)ÖнÏСµÄÒ»¸ö¶ÔÆë.²¢ÇҽṹµÄ³¤¶È±ØÐëΪËùÓùýµÄËùÓжÔÆë²ÎÊýµÄÕûÊý±¶,²»¹»¾Í²¹¿Õ×Ö½Ú.
S1ÖÐ,³ÉÔ±aÊÇ1×Ö½ÚĬÈÏ°´1×Ö½Ú¶ÔÆë,Ö¸¶¨¶ÔÆë²ÎÊýΪ8,ÕâÁ½¸öÖµÖÐÈ¡1,a°´1×Ö½Ú¶ÔÆë;³ÉÔ±bÊÇ4¸ö×Ö½Ú,ĬÈÏÊÇ°´4×Ö½Ú¶ÔÆë,Õâʱ¾Í°´4×Ö½Ú¶ÔÆë,ËùÒÔsizeof(S1)Ó¦¸ÃΪ8;
S2 ÖÐ,cºÍS1ÖеÄaÒ»Ñù,°´1×Ö½Ú¶ÔÆë,¶ød ÊǸö½á¹¹,ËüÊÇ8¸ö×Ö½Ú,Ëü°´Ê²Ã´¶ÔÆëÄØ?¶ÔÓڽṹÀ´Ëµ,ËüµÄĬÈ϶ÔÆ뷽ʽ¾ÍÊÇËüµÄËùÓгÉԱʹÓõĶÔÆë²ÎÊýÖÐ×î´óµÄÒ»¸ö,S1µÄ¾ÍÊÇ4.ËùÒÔ,³ÉÔ±d¾ÍÊÇ°´4×Ö½Ú¶ÔÆë.³ÉÔ±eÊÇ8¸ö×Ö½Ú,ËüÊÇĬÈÏ°´8×Ö½Ú¶ÔÆë,ºÍÖ¸¶¨µÄÒ»Ñù,ËùÒÔËü¶Ôµ½8×ֽڵı߽çÉÏ,Õâʱ, ÒѾʹ
Ïà¹ØÎĵµ£º
µ±½ñÓÐÐí¶àÃâ·ÑµÄ RDBMS£¨Relational Database Management
System£¬¹ØϵÊý¾Ý¿â¹ÜÀíϵͳ£©£¬ÆäÖÐһЩÊÇ¿ª·ÅÔ´ÂëÈí¼þ£¬ÁíһЩÊÇÉÌÒµ³§ÉÌÌṩµÄÃâ·Ñ²úÆ·¡£Èç¹ûÄúÕýÔÚʹÓà C/C++¡¢Java™¡¢.NET
»ò PHP
¿ª·¢Ó¦ÓóÌÐò£¬ÏëÑ°ÕÒÒ»ÖÖ¿É¿¿µÄÊý¾Ý·þÎñÆ÷£¬Ï£ÍûËüÖ§³ÖÏȽøµÄ¼¼Êõ¡¢¾ßÓ⻶ÏÀ©´óµÄ¿ª·¢ÈËÔ±ÉçÇø²¢ÒѾ²¿ÊðÔÚÐí¶à¹Ø¼ ......
ÕâƪÎÄÕÂÊÇʹÓÃSQLite C/C++½Ó¿ÚµÄÒ»¸ö¸ÅÒª½éÉܺÍÈëÃÅÖ¸ÄÏ¡£
ÓÉÓÚÔçÆÚµÄSQLiteÖ»Ö§³Ö5¸öC/C++½Ó¿Ú£¬Òò¶ø·Ç³£ÈÝÒ×ѧϰºÍʹÓ㬵«ÊÇËæ×ÅSQLite¹¦ÄܵÄÔöÇ¿£¬ÐµÄC/C++½Ó¿Ú²»¶ÏµÄÔö¼Ó½øÀ´£¬µ½ÏÖÔÚÓг¬¹ý150¸ö²»Í¬µÄAPI½Ó¿Ú¡£ÕâÍùÍùʹ³õѧÕßÍû¶øÈ´²½¡£ÐÒÔ˵ÄÊÇ£¬´ó¶àÊýSQLiteÖеÄC/C++½Ó¿ÚÊÇרÓõģ¬Òò¶øºÜÉÙ±»Ê¹Óõ½¡£¾¡¹ÜÓÐÕâ ......
char dec2hexChar(short int n) {
if ( 0 <= n && n <= 9 ) {
return char( short('0') + n );
} else if ( 10 <= n && n <= 15 ) {
return char( short('A') + n - 10 );
} else {
return char(0);
}
}
short int h ......
µ±ÔÚC Öж¨ÒåÁËÒ»¸ö½á¹¹ÀàÐÍʱ£¬ËüµÄ´óСÊÇ·ñµÈÓÚ¸÷×Ö¶Î(field) ´óС֮ºÍ£¿±àÒëÆ÷½«ÈçºÎÔÚÄÚ´æÖзÅÖÃÕâЩ×ֶΣ¿ANSI C¶Ô½á¹¹ÌåµÄÄÚ´æ²¼¾ÖÓÐʲôҪÇ󣿶øÎÒÃǵijÌÐòÓÖÄÜ·ñÒÀÀµÕâÖÖ²¼¾Ö£¿ÕâЩÎÊÌâ»òÐí¶Ô²»ÉÙÅóÓÑÀ´Ëµ»¹ÓеãÄ£ºý£¬ÄÇô±¾ÎľÍÊÔ×Å̽¾¿ËüÃDZ³ºóµÄÃØÃÜ¡£
Ê×ÏÈ£¬ÖÁÉÙÓÐÒ»µã¿ÉÒԿ϶¨£¬ÄǾÍÊÇANSI C ±£Ö¤½á¹¹ÌåÖи÷× ......
ÓÉÓÚ³ÌÐòÔËÐÐʱռÓõÄÄÚ´æ¹ý´ó£¬ËùÒÔÏë°ì·¨¸ø³ÌÐòÊÝÉí¡£
ÔÚµ÷ÊÔÖз¢ÏֽṹÌåÕ¼ÓõÄsize¾¹È»ºÍÔ¤ÏëµÄ²»Ò»Ñù£¬ÔÀ´……
¿´¿´ÏÂÃæ½²µÄ°É£¬¿Ï¶¨»á²»Í÷´Ë¿´Å¶£¡
1,±ÈÈç:
struct{
short a1;
short a2;
short a3;
}A;
struct{
long a1;
short a2;
}B;
sizeof(A)=6, sizeof(B)=8,Ϊʲô£¿
×¢£ºsizeof(sho ......