老斋
不尽人事,焉知天命。---- 榊眞喜男

原址:http://www.cppblog.com/mzty/archive/2005/10/24/832.html

 //  sizeof.cpp : Defines the entry point for the console application.
 
//
#include  " stdafx.h " 
#include   
< iostream > 
using   namespace  std;
struct  st 
      
{
         
short  num;
         
float  math_grade;
         
float  Chinese_grade;
         
float  sum_grade;
    }
 ;
void  main()
{
    cout 
<< " sizeof('$')= " << sizeof ( ' $ ' ) << endl;    
    cout 
<< " sizeof(1)= " << sizeof ( 1 ) << endl;
    cout 
<< " sizeof(1.5)= " << sizeof ( 1.5 ) << endl;  
    cout 
<< " sizeof(Good!)= " ;
    cout 
<< sizeof ( " Good! " ) << endl; 
     
int  i = 100 ;
     
char  c = ' A ' ;
     
float  x = 3.1416 ; 
     
double  p = 0.1 ;
    cout 
<< " sizeof(i)= " << sizeof (i) << endl;
    cout 
<< " sizeof(c)= " << sizeof (c) << endl;
    cout 
<< " sizeof(x)= " << sizeof (x) << endl;
    cout 
<< " sizeof(p)= " << sizeof (p) << endl; 
    cout 
<< " sizeof(x+1.732)= " << sizeof (x + 1.732 ) << endl;
    cout 
<< " sizeof(char)= " << sizeof ( char ) << endl;
    cout 
<< " sizeof(int)= " << sizeof ( int ) << endl;
    cout 
<< " sizeof(float)= " << sizeof ( float ) << endl;
    cout 
<< " sizeof(double)= " << sizeof ( double ) << endl;
     
char  str[] = " This is a test. " ;
     
int  a[ 10 ]; 
     
double  xy[ 10 ];
    cout 
<< " sizeof(str)= " << sizeof (str) << endl;
    cout 
<< " sizeof(a)= " << sizeof (a) << endl;
    cout 
<< " sizeof(xy)= " <<   sizeof (xy) << endl;
    
    st student1;
    cout 
<< " sizeof(st)= " << sizeof (st) << endl;
    cout 
<< " sizeof(student1)= " << sizeof (student1);
}
 

----------------------------the result are:-------------------------------------
sizeof('$')=1
sizeof(1)=4
sizeof(1.5)=8
sizeof("Good!")=6
sizeof(i)=4
sizeof(c)=1
sizeof(x)=4
sizeof(p)=8
sizeof(x+1.732)=8
sizeof(char)=1
sizeof(int)=4
sizeof(float)=4
sizeof(double)=8
sizeof(str)=16
sizeof(a)=40
sizeof(xy)=80
sizeof(st)=16
sizeof(student1)=16
Press any key to continue
-------------------------------------------------------------------------
//  #pragma pack( ) 
//  mulbayes 
//  unicode
-------------------------------------------------------------------------



为了能使CPU对变量进行高效快速的访问,变量的起始地址应该具有某些特性,
即所谓的“对齐”。例如对于4字节的int类型变量,其起始地址应位于4字节边界上,
即起始地址能够被4整除。变量的对齐规则如下(32位系统):


Type
Alignment

char
在字节边界上对齐

short (16-bit)
在双字节边界上对齐

int and long (32-bit)
在4字节边界上对齐

float
在4字节边界上对齐

double
在8字节边界上对齐

 

structures
单独考虑结构体的个成员,它们在不同的字节边界上对齐。
其中最大的字节边界数就是该结构的字节边界数。
MSDN原话:Largest alignment requirement of any member
理解结构体的对齐方式有点挠头,如果结构体中有结构体成员,
那么这是一个递归的过程。
对齐方式影响结构体成员在结构体中的偏移设编译器设定的最大对齐字节边界数为n,
对于结构体中的某一成员item,它相对于结构首地址的实际字节对齐数目X应该满足
以下规则:

X = min(n, sizeof(item))

例如,对于结构体 struct {char a; int b} T;

当位于32位系统,n=8时:
a的偏移为0,
b的偏移为4,中间填充了3个字节, b的X为4;

当位于32位系统,n=2时:
a的偏移为0,
b的偏移为2,中间填充了1个字节,b的X为2;

结构体的sizeof
设结构体的最后一个成员为LastItem,其相对于结构体首地址的
偏移为offset(LastItem),其大小为sizeof(LastItem),结构体的字节对齐数为N,
则:结构体的sizeof 为: 若offset(LastItem)+ sizeof(LastItem)能够被N整除,
那么就是offset(LastItem)+ sizeof(LastItem),否则,在后面填充,
直到能够被N整除。

例如:32位系统,n=8,
结构体 struct {char a; char b;} T;
struct {char a; int b;} T1;
struct {char a; int b; char c;} T2;
sizeof(T) == 2; N = 1 没有填充
sizeof(T) == 8; N = 4 中间填充了3字节
sizeof(T2)==12; N = 4 中间,结尾各填充了3字节

注意:

1) 对于空结构体,sizeof == 1;因为必须保证结构体的每一个实例在内存中都
有独一无二的地址。

2) 结构体的静态成员不对结构体的大小产生影响,因为静态变量的存储位置与
结构体的实例地址无关。

例如:

struct {static int I;} T; struct {char a; static int I;} T1;
sizeof(T) == 1; sizeof(T1) == 1;

3) 某些编译器支持扩展指令设置变量或结构的对齐方式,如VC,
  详见MSDN(alignment of structures)

 

并不是要求#pragma pack(8),就一定是每个成员都是8字节对齐
而是指一组成员要按照8字节对齐。
struct s1
{
   short a;   // 2字节
   long b;    // 4字节
};
整个s1小于8字节,因此s1就是8字节。

struct s2
{
   char c;    // 1字节
   s1 d;      // 8字节
   __int64 e; // 8字节
};
整个s2小于12字节,但是由于#pragma pack(8)的限定,12不能与8字节对齐,因此s2就是24字节,c占用8字节
---------------------------------


类或对象的长度:
   非虚函数相当与全局,不在类里。
   静态也是全局,不在类里。
   但是const要分配空间。


非静态变量,虚函数链表(如果类中有虚函数的话) -----------分配空间

posted on 2009-04-06 13:00 老斋 阅读(218) 评论(0)  编辑 收藏 引用 所属分类: C/C++算法

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理