易截截图软件、单文件、免安装、纯绿色、仅160KB

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:

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


相关文档:

用Eclipse搭建C/C++开发平台

谈到Eclipse这个开源IDE,大家都会潜意识地把它和JAVA开发联系起来。没错,Eclipse用于JAVA application的开发是目前的主流,而且它本身也是需要JRE才能运行的,因而无论怎么看Eclipse都与JAVA有密不可分的联系。 但实际上Eclipse只是一个开发环境,一个为程序员提供的框架,与语言并无直接联系。自从2001年IBM将Eclipse捐� ......

在vc.net中的C/C++代码生成DLL步骤


1 选择new->project->win32 console project;
    在这一步的选框上有一个选项是 create dictionary for solution, 我不知道这个具体是什么作用,选了之后会项目出现两层文件夹,比如你创建一个命名为test的项目,会生成test文件夹,test中包含另一个test文件夹,你的项目实际上放在了内层te ......

C开发中堆和栈的差别

1,栈区(stack)—   由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 例如,声明在函数中的一个局部变量int b;系统自动在栈中为b开辟空间。只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出。比如:
char* AllocStrfromStack ......

C/C++数组名与指针区别深层探索

作者:宋宝华 e-mail:21cnbao@21cn.com
1.    引言
指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用。于是乎,很多程序设计者就被搞糊涂了。而许多的大学老师,他们在C语言的教学过程中也错误得给学生讲解:“数组名就是指针”。很幸运,我的大学老 ......

C/C++文件操作[转载]

掌握文本文件读写的方法
了解二进制文件的读写方法
C++文件流:
fstream  // 文件流
ifstream  // 输入文件流
ofstream  // 输出文件流
//创建一个文本文件并写入信息
//同向屏幕上输出信息一样将信息输出至文件
#include<iomanip.h>
#include<fstream.h>
void main()
{
  ofstream f1("d:\\ ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号