您的位置:首页 > 其它

关于零长度数组

2006-08-29 16:47 309 查看
struct test {
int inode;
char name[0];
};
这样定义后sizeof(struct test) 一直是4,
name这个变量要是赋值的话, 其地址比test高4字节的地方 ,所以这样就方便结构体的使用者灵活使用,一般每次alloc多一点空间留给私有数据,name放到最后,name地址比结构体最高地址高4字节,所以name就指向结构体后面的空间(不管是在堆栈 还是 heap里)

name应该是在其上面的变量的地址的下一个空间
typedef struct guhuo{
unsigned int a;
unsigned char data1[0];
unsigned int b;
}zqy;
则data1的地址就和b一样了;

int x;
struct test t1;
int y;
x地址在t1上面,所以name地址就和x一样。

内核中有关代码如下
nt ip_options_get(struct ip_options **optp, unsigned char *data, int optlen, int user)
{
struct ip_options *opt;

opt = kmalloc(sizeof(struct ip_options)+((optlen+3)&~3), GFP_KERNEL);
if (!opt)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: