Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1830194
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类: LINUX

2008-01-11 12:32:02

From ULNI, thanks to kaikai :)

Kernel中的一些数据结构结尾包含一个optional block,例如
        struct ob
        {
                int a;
                char placeholder[0];
        };
下面的代码中给出了解释:

$ cat optionalBlock.c
#include <stdio.h>
#include <stdlib.h>

int main()
{
        struct ob
        {
                int a;
                char placeholder[0];
        };

        struct ob * non_ob = malloc(sizeof(struct ob));
        struct ob * with_ob = malloc(sizeof(struct ob)+10);

        printf("sizeof(struct ob) = %d\n", sizeof(struct ob));
        printf("non_ob starts at: %x\n", non_ob);
        printf("The address of non_ob->placeholder[0]: %x\n", &(non_ob->placeholder[0]));

        printf("with_ob starts at: %x\n", with_ob);
        printf("The address of with_ob->placeholder[0]: %x\n", &(with_ob->placeholder[0]));
        printf("The address of with_ob->placeholder[9]: %x\n", &(with_ob->placeholder[9]));
        free(non_ob);
        free(with_ob);
}

$ gcc optionalBlock.c
$ ./a.out
sizeof(struct ob) = 4
non_ob starts at: 8049748
The address of non_ob->placeholder[0]: 804974c
with_ob starts at: 8049758
The address of with_ob->placeholder[0]: 804975c
The address of with_ob->placeholder[9]: 8049765


在没有分配optionalblock的时候,placeholder[0]不占地方,那个struct ob只有int大小.同时,访问placeholder的地址,是non_ob的末尾.
在分配了optionalblock的时候,placeholder相当于给出了一个扩展的char数组.大小为sizeof(struct ob)之后加的那个值,从地址也可以看出.
这样设计的好处在于,可以从一个数据结构中扩展出所需的额外的东西,避免重复实现差别很小的数据结构,类似于OO的继承中,子类添加少量的数据结构.
另外一个好处,by kaikai,是节约内存。:)

参考gcc关于Zero-Length Array的文档: //gcc的扩展


阅读(1393) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~