1、结构体内存布局(padding)
为了让CPU能够更舒服地访问到变量,struct中的各成员变量的存储地址有一套对齐的机制。这个机制概括起来有两点:第一,每个成员变量的首地址,必须是它的类型的对齐值的整数倍,如果不满足,它与前一个成员变量之间要填充(padding)一些无意义的字节来满足;第二,整个struct的大小,必须是该struct中所有成员的类型中对齐值最大者的整数倍,如果不满足,在最后一个成员后面填充。
The following typical alignments are valid for compilers from Microsoft, Borland, and GNU when compiling for 32-bit x86:
- A char (one byte) will be 1-byte aligned.
- A short (two bytes) will be 2-byte aligned.
- An int (four bytes) will be 4-byte aligned.
- A float (four bytes) will be 4-byte aligned.
- A double (eight bytes) will be 8-byte aligned on Windows and 4-byte aligned on Linux.
- A long double (twelve bytes) will be 4-byte aligned on Linux.
- Any pointer (four bytes) will be 4-byte aligned on Linux. (eg: char*, int*)
The only notable difference in alignment for a 64-bit linux system when compared to a 32 bit is:
- A double (eight bytes) will be 8-byte aligned.
- A long double (Sixteen bytes) will be 16-byte aligned.
- Any pointer (eight bytes) will be 8-byte aligned.
例子:
比如这个结构体,占用的并不是5个字节,而是8个字节。因为number成员的偏移量是4,其前面偏移了3个字节的偏移量
struct arr_data
{
char h;
int number;
};
2.变长数组
摘要:在实际的编程中,我们经常需要使用变长数组,但是C语言并不支持变长的数组。此时,我们可以使用结构体的方法实现C语言变长数组。
struct MyData
{
int nLen;
char data[0];
};
在结构中, data是一个数组名;但该数组没有元素 ; 该数组的真实地址紧随结构体MyData之后,而这个地址就是结构体后面数据的地址(如果给这个结构体分配的内容大于这个结构体实际大小,后面多余的部分就是这个data的内容) ;这种声明方法可以巧妙的实现C语言里的数组扩展。
实际用时采取这样:
struct MyData *p = (struct MyData *)malloc(sizeof(struct MyData )+strlen(str))
这样就可以通过p->data 来操作这个str。
这样就可以通过p->data 来操作这个str。
struct MyData
{
int nLen;
char data[0];
};
int main()
{
int nLen = 10;
char str[10] = "123456789";
cout << "Size of MyData: " <<sizeof(MyData) << endl;
MyData *myData = (MyData*)malloc(sizeof(MyData) +10);
memcpy(myData->data, str, 10);
cout << "myData's Data is: " << myData->data << endl;
free(myData);
return 0;
}
//output:
Size of MyData:4
myData"s Data is: 123456789