下面的程序主要是测试struct的大小,另外注意到在gcc4.3.3编译时,得到堆中的内存块,即firstNode.data和psbNode指向的内存,每个字节默认自动初始化为0值;而在VS2008编译下,每个字节被初始化为0xcd。
在struct DemoStruct{char cArray[];};DemoStruct的大小不包含从cArray的大小,这种结构中的不定长数组不被计入结构体的大小。这就是C语言的不定内存问题。
[希望了解的朋友去查看一下gcc文档,是不是自动初始化堆内存的每个字节为0,如果此处有错,希望给出正确的解答!]
// 测试两种struct结构在指定对齐和默认对齐下的内存占用大小
// 此源自blueshycool的blog: http://blog.chinaunix.net/u/18584/showart_296056.html
// Created by LHJ at 2009-11-21
#include
#include
#include
#define SIZE 10
#pragma pack(1)
typedef struct FirstBlock_t
{
int size;
char *data;
}FirstBlock;
typedef struct SecondBlock_t
{
int size;
char data[1];
}SecondBlock;
void StructTest(void)
{
FirstBlock firstNode;
firstNode.size = SIZE;
firstNode.data = (char*)malloc(firstNode.size * sizeof(char));
SecondBlock *psbNode = (SecondBlock*)malloc(sizeof(SecondBlock) + SIZE);
psbNode->size = SIZE;
printf("\n\n");
printf("------------- firstNode Information ----------------------\n");
printf("offset: \n");
printf("\toffsetof(FirstBlock, size): %d\n", offsetof(FirstBlock, size));
printf("\toffsetof(FirstBlock, data): %d\n", offsetof(FirstBlock, data));
printf("\tsizeof(FirstBlock): %d\n", sizeof(FirstBlock));
printf("content: \n");
printf("\tfirstNode.data[%d] Ascii: [%d]\n", firstNode.size-1, firstNode.data[firstNode.size-1]);
printf("address info: \n");
printf("\tfirstNode.data: 0x%08x\n", (unsigned int)firstNode.data);
printf("\t&firstNode.data[0]: 0x%08x\n", (unsigned int)&firstNode.data[0]);
printf("\t&firstNode.data[%d]: 0x%08x\n", (unsigned int)firstNode.size-1,
(unsigned int)&firstNode.data[firstNode.size-1]);
printf("\n-------------- *psbNode Information ----------------------\n");
printf("offset: \n");
printf("\toffsetof(*psbNode, size): %d\n", offsetof(SecondBlock, size));
printf("\toffsetof(*psbNode, data): %d\n", offsetof(SecondBlock, data));
printf("\tsizeof(SecondBlock): %d\n", sizeof(SecondBlock));
printf("content: \n");
printf("\tpsbNode->data[%d] Ascii: [%d]\n", psbNode->size-1, psbNode->data[psbNode->size-1]);
printf("address info: \n");
printf("\tpsbNode: 0x%08x\n", (unsigned int)psbNode);
printf("\t&psbNode->data[0]: 0x%08x\n", (unsigned int)&psbNode->data[0]);
printf("\tpsbNode->data[%d]: 0x%08x\n", (unsigned int)psbNode->size + 100000,
(unsigned int)psbNode->data[psbNode->size + 100000]);
// free malloc's mem
free(firstNode.data);
free(psbNode);
printf("\n-------------- After free ----------------------\n");
printf("address info: \n");
printf("\tfirstNode.data: 0x%08x\n", (unsigned int)firstNode.data);
// invalid use wild pointers
//printf("\t&firstNode.data[0]: 0x%08x\n", (unsigned int)&firstNode.data[0]);
printf("\tpsbNode: 0x%08x\n", (unsigned int)psbNode);
// invalid use wild pointers
//printf("\t&psbNode->data[0]: 0x%08x\n", (unsigned int)&psbNode->data[0]);
firstNode.data = NULL;
psbNode = NULL;
printf("after reset pointers: \n");
printf("\tfirstNode.data: 0x%08x\n", (unsigned int)firstNode.data);
printf("\tpsbNode: 0x%08x\n", (unsigned int)psbNode);
printf("\n\n");
}
int main(int argc, char *argv[])
{
StructTest();
}
#pragma pack()
此段程序在linux,gcc4.3.3环境中的结果:
------------- firstNode Information ----------------------
offset:
offsetof(FirstBlock, size): 0
offsetof(FirstBlock, data): 4
sizeof(FirstBlock): 8
content:
firstNode.data[9] Ascii: [0]
address info:
firstNode.data: 0x095a0008
&firstNode.data[0]: 0x095a0008
&firstNode.data[9]: 0x095a0011
-------------- *psbNode Information ----------------------
offset:
offsetof(*psbNode, size): 0
offsetof(*psbNode, data): 4
sizeof(SecondBlock): 5
content:
psbNode->data[9] Ascii: [0]
address info:
psbNode: 0x095a0018
&psbNode->data[0]: 0x095a001c
psbNode->data[100010]: 0x00000000
-------------- After free ----------------------
address info:
firstNode.data: 0x095a0008
psbNode: 0x095a0018
after reset pointers:
firstNode.data: 0x00000000
psbNode: 0x00000000
阅读(994) | 评论(0) | 转发(0) |