免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 763 | 回复: 0
打印 上一主题 下一主题

optional block [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-11 12:32 |只看该作者 |倒序浏览

                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的扩展
http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html#Zero-Length
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/12325/showart_461513.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP