关于结构体中最后的 char */char []

在看redis源码的时候看到一个strcut sdshdr ,如下:

struct sdshdr {
int len;
int free;
char buf[];
};

代码中对于这个结构体的大小的处理挺奇怪的,试验一下,实验环境为VC6.0,求sizeof大小

struct sdshdr {
int len;
int free;
char *buf;
};

大小是12


struct sdshdr {
int len;
int free;
char buf[];
};
大小为8

struct sdshdr {
int len;
int free;
char buf[0];
};

大小为8


struct sdshdr {
int len;
int free;
char buf[1];
};
大小为12

说明在结构体里面, char* buf 和char buf[1]的效果差不多(这个是在对齐的情况下,不对齐的话也是不一样的),占4个字节;
char buf[0] 和char buf[]是一样的,不占内存。

 

posted on 2012-07-06 15:06  aitilang  阅读(792)  评论(0编辑  收藏  举报

导航