文章标题struct对齐方式

//用一个宏定义FIND求结构体struct s中某个成员变量member相对struct s的偏移量.
//思考:若struct s的地址为0,则其成员member的地址就是其相对于s的偏移量
//扩展: <1>sizeof(struct s) 不一定等于 sizeof(struct s中的每一个成员)的和
// <2>结构体大小不仅由成员的大小决定(sizeof(member)),而且还要考虑编译器用来优化的对齐。
// 编译器的优化对齐(alignment), 是为了减少访问总线的次数。
//由于数据总线的位数(由机器字长决定),为了减少数据访问的次数。
//这样可以保证机器字长整数倍的基本类型(而非自定义类型)
//能够通过 sizeof(基本类型)/sizeof(size_t)次总线访问完成;
//通过实验, 我得出的对齐规则是:
// char 偏移地址二进制的最后一位:0/1;即1的倍数的地址对齐性;即任意对齐性;
// short 偏移地址二进制的最后一位:0; 即2的倍数地址的对齐性;即偶数地址对齐性;
// int 偏移地址二进制的最后两位:00; 即4的倍数的地址对齐性;
// float 偏移地址二进制的最后两位:00; 即4的倍数的地址对齐性;
// long 偏移地址二进制的最后两位:00; 即4的倍数的地址对齐性;
// long long 偏移地址二进制的最后三位:000;即8的倍数的地址对齐性;
// double偏移地址二进制的最后三位:000;即8的倍数的地址对齐性;
//总之,struct中基本类型成员的偏移地址时一定是sizeof(基本类型)的整数倍,
//自定义类型偏移地址的对齐性是任意的.
//推过这种方式,就能解释sizeof(struct s)不等于所有成员的sizeof的和。
// sizeof(struct s) 除了满足以上偏移地址对齐外,
// 是其大小为其基本类型成员的sizeof值最大的成员的sizeof值的整数倍
// 也就是说为最大的基本类型的sizeof值得整数倍。
程序举例:

#include <iostream>  

using namespace std;  


struct cc {  
    char a;  
    char c[6];  
    char b;  
};  
struct student {  
    int a;  
    char b[20];  
    char c;  
    struct cc cc;  
    double d;  
};  

//用一个宏定义FIND求结构体struct s中某个成员变量member相对struct s的偏移量.
//思考:若struct s的地址为0,则其成员member的地址就是其相对于s的偏移量
//扩展: <1>sizeof(struct s) 不一定等于 sizeof(struct s中的每一个成员)的和
// <2>结构体大小不仅由成员的大小决定(sizeof(member)),而且还要考虑编译器用来优化的对齐。
// 编译器的优化对齐(alignment), 是为了减少访问总线的次数。
//由于数据总线的位数(由机器字长决定),为了减少数据访问的次数。
//这样可以保证机器字长整数倍的基本类型(而非自定义类型)
//能够通过 sizeof(基本类型)/sizeof(size_t)此总线访问完成;
//通过实验, 我得出的对齐规则是:
// char 偏移地址二进制的最后一位:0/1;即1的倍数的地址对齐性;即任意对齐性;
// short 偏移地址二进制的最后一位:0; 即2的倍数地址的对齐性;即偶数地址对齐性;
// int 偏移地址二进制的最后两位:00; 即4的倍数的地址对齐性;
// float 偏移地址二进制的最后两位:00; 即4的倍数的地址对齐性;
// long 偏移地址二进制的最后三位:000;即8的倍数的地址对齐性;
// double偏移地址二进制的最后三位:000;即8的倍数的地址对齐性;
//总之,struct中基本类型成员的偏移地址时一定是sizeof(基本类型)的整数倍,
//自定义类型偏移地址的对齐性是任意的.
//推过这种方式,就能解释sizeof(struct s)不等于所有成员的sizeof的和。
// sizeof(struct s) 除了满足以上偏移地址对齐外,
// 是其大小为其基本类型成员的sizeof值最大的成员的sizeof值的整数倍
// 也就是说为最大的基本类型的sizeof值得整数倍。

#define FIND(s, member)  (size_t)(&((*((s *)((void *)0))).member))  
#define FIND1(s, member) (size_t)(&(*((s *)0)).member)  
#define FIND2(s, member) (size_t)(&(((s *)0)->member))  

#define DISPLACEMENT1(structure, member) (size_t) &((*((structure *)0)).member)  
#define DISPLACEMENT2(structure, member) (size_t) &(((structure *)0)->member)  

struct c {  
    char a;     //1字节 [0, 0]  
    char c[5];  //5字节 [1, 5]  
 //char cc[2];  //填充2字节 [6, 7]  
                //8字节  

    int  b;    //12字节 [8, 11]  

    char d;    //14字节 [12, 13] //填充一字节  
    short e;   //16字节 [14, 15]  

    char f;    //17字节 [16, 16]  
    char g;    //18字节 [17, 17]  
    char h;    //19字节 [18, 18]  
    //char i;  //20字节  [19, 19]  
    short s;   //24字节 [20, 23]  
};  

struct ccc {  
    char a;     // 0  
    int  b;     // 4  
    char c[5];  // 8  
    char d;     // 13  
    double e;   // 16  
    char f;     // 24  
    char g;     // 25  
    //int h;     // 28  
};  


struct d {  
    char a;  //0  
    short b; //2  
    int c;   //4  
    char d;  //8  
    double f;//16  
};  

struct e {  
    char a;   //0  
    double b; //8  
    char c;   //16  
};           //24  

struct f {  
    char a;   // 0  
    float b;  // 4  
    char c;   // 8  
};  

struct g {  
    char a;  
    short b;  
    char c;  
};  
         // 12  
int main()  
{  
    cout << FIND(student, a) << endl;  
    cout << FIND(student, b) << endl;  
    cout << FIND(student, c) << endl;  
    cout << FIND(student, d) << endl;  
    cout << endl;  

    cout << FIND1(student, a) << endl;  
    cout << FIND1(student, b) << endl;  
    cout << FIND1(student, c) << endl;  
    cout << FIND1(student, d) << endl;  
    cout << endl;  


    cout << FIND2(student, a) << endl;  
    cout << FIND2(student, b) << endl;  
    cout << FIND2(student, c) << endl;  
    cout << FIND2(student, d) << endl;  
    cout << endl;  





    cout << DISPLACEMENT1(student, a) << endl;  
    cout << DISPLACEMENT1(student, b) << endl;  
    cout << DISPLACEMENT1(student, c) << endl;  
    cout << DISPLACEMENT1(student, d) << endl;  
    cout << endl;  


    cout << DISPLACEMENT2(student, a) << endl;  
    cout << DISPLACEMENT2(student, b) << endl;  
    cout << DISPLACEMENT2(student, c) << endl;  
    cout << DISPLACEMENT2(student, cc) << endl;  
    cout << DISPLACEMENT2(student, d) << endl;  
    cout << "sizeof(student) = " << sizeof(student) << endl;  
    cout << endl;  

    cout << DISPLACEMENT2(c, a) << endl;  
    cout << DISPLACEMENT2(c, b) << endl;  
    cout << DISPLACEMENT2(c, c) << endl;  
    cout << DISPLACEMENT2(c, d) << endl;  
    cout << DISPLACEMENT2(c, e) << endl;  
    cout << DISPLACEMENT2(c, f) << endl;  
    cout << DISPLACEMENT2(c, g) << endl;  
    cout << DISPLACEMENT2(c, h) << endl;  
   // cout << DISPLACEMENT2(c, i) << endl;  
    cout << DISPLACEMENT2(c, s) << endl;  
    cout << "sizeof(c) = " << sizeof(c) << endl;  
    cout << "sizeof(c.c) = " << sizeof(((c*) 0)->c) << endl;  
    cout << endl;  

    cout << DISPLACEMENT2(ccc, a) << endl;  
    cout << DISPLACEMENT2(ccc, b) << endl;  
    cout << DISPLACEMENT2(ccc, c) << endl;  
    cout << DISPLACEMENT2(ccc, d) << endl;  
    cout << DISPLACEMENT2(ccc, e) << endl;  
    cout << DISPLACEMENT2(ccc, f) << endl;  
    cout << DISPLACEMENT2(ccc, g) << endl;  
    //cout << DISPLACEMENT2(ccc, h) << endl;  
    cout << "sizeof(ccc) = " << sizeof(ccc) << endl;  
    cout << endl;  

    student s;  
    cout << sizeof(s.cc) << endl; //you  
    cout << sizeof(c) << endl;  
    cout << sizeof(ccc) << endl;  


    cout << "sizeof(d) = "  << sizeof(d) << endl;  
    cout << DISPLACEMENT2(d, a) << endl;  
    cout << DISPLACEMENT2(d, b) << endl;  
    cout << DISPLACEMENT2(d, c) << endl;  
    cout << DISPLACEMENT2(d, d) << endl;  
    cout << DISPLACEMENT2(d, f) << endl;  
    cout << endl;  

    cout << endl;  
    cout << "sizeof(cc) = " << sizeof(cc) << endl;  
    cout << "sizeof(student) = " << sizeof(student) << endl;  
    cout << "sizeof(ccc) = " << sizeof(ccc)  << endl;  
    cout << "sizeof(c) = " << sizeof(c) << endl;  
    cout << "sizeof(d) = " << sizeof(d) << endl;  
    cout << "sizeof(e) = " << sizeof(e) << endl;  
    cout << "sizeof(f) = " << sizeof(f) << endl;  
    cout << "sizeof(g) = " << sizeof(g) << endl;  

    cout << "Hello world!" << endl;  
    return 0;  
}  

补充:
首先要明确sizeof不是函数,也不是一元运算符,//这个有待商榷,因为运算符优先级里有它。
他是个类似宏定义的特殊关键字,sizeof();
括号内在编译过程中是不被编译的,而是被替代类型,
如 int a=8;sizeof(a);
在编译过程中,它不管a的值是什么,只是被替换成类型 sizeof(int); 结果为4.
如果sizeof(a=6);呢,也是一样的转换成a的类型,但是要注意 因为a=6是不被编译的,所以执行完sizeof(a=6);a的值还是8,是不变的!

记住以下几个结论:
1.unsigned影响的只是最高位bit的意义(正负),数据长度不会被改变的。所以sizeof(unsigned int) == sizeof(int);
2.自定义类型的sizeof取值等同于它的类型原形。如typedef short WORD;sizeof(short) == sizeof(WORD)。
3.对函数使用sizeof,在编译阶段会被函数返回值的类型取代int f1(){return 0;};
cout <

char a[] = “abcdef “;
char b[20] = “abcdef “;
string s = “abcdef “;
cout << strlen(a) << endl; // 6,字符串长度
cout << sizeof(a) << endl; // 7,字符串容量
cout << strlen(b) << endl; // 6,字符串长度
cout << sizeof(b) << endl; // 20,字符串容量
cout << sizeof(s) << endl; //4, 这里不代表字符串的长度,而是string类的大小
cout << strlen(s) << endl; // 错误!s不是一个字符指针。
cout << strlen(s.c_str()) << endl;
a[1] = '\0';
cout < <strlen(a) < <endl; // 1
cout < <sizeof(a) < <endl; // 7,sizeof是恒定的

参考 :http://en.wikipedia.org/wiki/Data_structure_alignment
http://www.ibm.com/developerworks/library/pa-dalign/

在C语言中,结构体的字节对齐是为了优化内存访问速度和对齐要求而进行的一种对齐方式结构体的字节对齐确保结构体中的成员按照一定的规则进行排列,以便于处理器高效地访问内存。 结构体的字节对齐规则通常由编译器根据特定的对齐选项和目标平台的要求来确定。在C语言中,可以使用`#pragma pack`指令或者编译器提供的特定选项来控制结构体的字节对齐方式。 默认情况下,大多数编译器会按照特定的对齐规则进行字节对齐。这些规则通常是根据基本数据类型的大小来确定的。例如,常见的对齐规则是按照4字节对齐(即结构体成员的偏移量必须是4的倍数)或者8字节对齐。 以下是一个示例,展示了如何使用`#pragma pack`指令来设置结构体的字节对齐方式: ```c #pragma pack(push, 1) // 以1字节对齐 struct MyStruct { char c; int i; double d; }; #pragma pack(pop) // 恢复默认的对齐方式 int main() { printf("sizeof(MyStruct) = %zu\n", sizeof(struct MyStruct)); return 0; } ``` 在上面的示例中,`#pragma pack(push, 1)`指令将当前的对齐方式推入一个栈中,并将对齐方式设置为1字节对齐。然后定义了一个包含不同类型成员的结构体。最后,使用`#pragma pack(pop)`指令将对齐方式恢复为默认值。 请注意,修改结构体的字节对齐方式可能会导致内存浪费或者访问错误,因此在修改字节对齐方式时要特别小心。建议仅在必要时进行修改,并确保了解目标平台的字节对齐要求。 希望这能回答你的问题!如果还有疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值