about pack and align for C/C++
http://msdn.microsoft.com/en-us/library/2e70t5y1(VS.80).aspx
#pragma
pack( n )
n : Valid values are 1, 2, 4, 8, and 16.the
alignment of a member will be on a boundary that is either a multiple of
n
or
a multiple of the size of the member
,
whichever is smaller.
The
default value for n
is 8
³ÉÔ±±äÁ¿¶ÔÆë£ºÑ¡n
ºÍ¸Ã³ÉÔ±³¤¶È
µÄ
×îСֵ(µÄ±¶Êý)
½á¹¹ABCDÖÐdµÄ¶ÔÆë·½Ê½£¬Ñ¡nºÍsizeof(d)µÄ×îСֵ8£¬È»ºó°´8×Ö½Ú¶ÔÆë£¡£¡£¡
sizeof´óС£ºÑ¡n
ºÍ×î´ó³É
Ô±³¤¶È
µÄ×îСֵ£¬¶ÔÆë
Àý×Ó£º
#pragma
pack(8)
struct ABCD
{
bool
b;
short s;
double d;
};
int
main()
{
cout << offsetof(ABCD,
b) << endl;
cout << offsetof(ABCD,
s) << endl;
cout << offsetof(ABCD,
d) << endl;
cout << sizeof(ABCD) << endl;
}
output
is:
0
2
8
16
http://msdn.microsoft.com/en-us/library/83ythb65(VS.71).aspx
__declspec( align( n ) )
__declspec( align() ) ºÍ #pragma pack
ÊÇÒ»¶ÔÐֵܣ¬Ç°Õ߹涨ÁË¶ÔÆëµÄ×îСֵ£¬ºóÕ߹涨ÁË¶ÔÆëµÄ×î´óÖµ£¬Á½Õßͬʱ³öÏÖʱ£¬Ç°ÕßÓµÓиü¸ßµÄÓÅÏȼ¶¡£
Àý×Ó£º
#define
CACHE_ALIGN 64
// Force each structure to be
in a different cache line.
struct
__declspec(align(CACHE_ALIGN)) CUSTINFOAA {
DWORD
dwCustomerID; // Mostly read-only
wchar_t
szName[100]; // Mostly read-only
// Force the following members to be in a different cache line.
__declspec(align(CACHE_ALIGN))
int
nBalanceDue; // Read-write
FILETIME
ftLastOrderDate; // Read-write
};
int
main()
{
cout <<
offsetof(CUSTINFOAA, dwCustomerID) << endl;
cout
<< offsetof(CUSTINFOAA, szName) << endl;
cout
<< offsetof(CUSTINFOAA, nBalanceDue) << endl;
cout << offsetof(CUSTINFOAA, ftLastOrderDate) <<
endl;
cout << sizeof(CUSTIN
Ïà¹ØÎĵµ£º
֮ǰд¹ýÒ»¸öʵÏÖÁËijÖÖ¹¦ÄܵÄjava³ÌÐò£¬µ«ÓÉÓÚ½üÈÕ“¹¤×÷”ÐèÒª£¬ÓÖÐèÒªÔÚc/c++ÖÐÖØÐÂʹÓøù¦ÄÜ¡£ÎªÁ˽Úʡʱ¼ä£¬ÎÒ²ÉÈ¡ÁËÔÚc/c++Öе÷ÓÃjava³ÌÐòµÄ·½·¨£¬µ«Ö®Ç°Ã»ÓнӴ¥¹ýÀàËÆµÄ¶«Î÷£¬µ½ÍøÉÏÒ»²é£¬¹ûÈ»ÓÐÀàËÆµÄ¶«Î÷£¬ÐÄÖаµÏ²ing...¡£²éµ½µÄ×ÊÁÏÖÐÓм¸¸ö²»´íµÄÍøÒ³£º
&nbs ......
×öWindows³ÌÐò¿ª·¢£¬windows APIÒ»¶¨ÊÇҪѧϰµÄ¡£ËäÈ»ÏÖÔÚwindowsƽ̨ϵĿª·¢»·¾³Óкܶ࣬ҲºÜ·½±ã£¬µ«Í¨³£Ö»»áÌṩ
windows·þÎñµÄÒ»¸ö×Ó¼¯£¬ÓÐÐ©ÌØÐÔ»¹µÃµ÷ÓÃAPI¡£
·Ö²ãÊÇÈí¼þ¿ª·¢ÖÐÒ»¸öÓÀ¾ÃµÄ½â¾ö·½°¸£¬ºÜ¶àÄÑÒÔ½â¾öµÄÎÊÌâ¶¼¿ÉÒÔÓöà¼ÓÒ»²ãÀ´½â¾ö¡£²Ù×÷ϵͳ¾ÍÊÇÒ»¸öºÜºÃµÄÀý×Ó¡£²Ù×÷ϵͳÔÚ
Èí¼þ¿ª·¢ÈËÔ±Óë¼ÆËã»úÓ²¼þÖ ......
1£¬Õ»Çø£¨stack£©— ÓɱàÒëÆ÷×Ô¶¯·ÖÅäÊÍ·Å£¬´æ·Åº¯ÊýµÄ²ÎÊýÖµ£¬¾Ö²¿±äÁ¿µÄÖµµÈ¡£Æä²Ù×÷·½Ê½ÀàËÆÓÚÊý¾Ý½á¹¹ÖеÄÕ»¡£ ÀýÈ磬ÉùÃ÷ÔÚº¯ÊýÖеÄÒ»¸ö¾Ö²¿±äÁ¿int b£»ÏµÍ³×Ô¶¯ÔÚÕ»ÖÐΪb¿ª±Ù¿Õ¼ä¡£Ö»ÒªÕ»µÄÊ£Óà¿Õ¼ä´óÓÚËùÉêÇë¿Õ¼ä£¬ÏµÍ³½«Îª³ÌÐòÌṩÄڴ棬·ñÔò½«±¨Òì³£ÌáʾջÒç³ö¡£±ÈÈ磺
char* AllocStrfromStack ......
ÕÆÎÕÎı¾Îļþ¶ÁдµÄ·½·¨
Á˽â¶þ½øÖÆÎļþµÄ¶Áд·½·¨
C++ÎļþÁ÷£º
fstream¡¡¡¡// ÎļþÁ÷
ifstream¡¡ // ÊäÈëÎļþÁ÷
ofstream¡¡ // Êä³öÎļþÁ÷
//´´½¨Ò»¸öÎı¾Îļþ²¢Ð´ÈëÐÅÏ¢
//ͬÏòÆÁÄ»ÉÏÊä³öÐÅÏ¢Ò»Ñù½«ÐÅÏ¢Êä³öÖÁÎļþ
#include<iomanip.h>
#include<fstream.h>
void main()
{
¡¡¡¡ofstream f1("d:\\ ......
VC++
ÓëVB
Êý¾ÝÀàÐͶÔÓ¦¹ØÏµ
ÔÚ×öVCÓëvb³ÌÐò¼ä»¥Ïàµ÷Óã¬ÐèҪעÒâÁ½ÖÖÓïÑÔ²»Í¬±àÒëÆ÷¶ÔÊý¾ÝÀàÐ͵͍ÒåÇø±ð£¬´ËʱÁ½ÕßÊý¾ÝÀàÐ͵ĶÔÓ¦¹ØÏµ¾ÍÏÔµÃÊ®·ÖµÄÖØÒª£¬¶ÔÓ¦¹ØÏµÒÔ¼°ÉùÃ÷·½Ê½ÈçÏÂËùʾ¡£
VC++
VB
short
Integer
int
Long
long
Long
UNIT
Long
ULONG
Long
WORD
DWORDLon ......