现在的位置: 首页 > 综合 > 正文

GNU C中的零长度数组

2013年06月07日 ⁄ 综合 ⁄ 共 1119字 ⁄ 字号 评论关闭

随手翻阅"linux内核设计与实现"一书,看到一个关于进程的结构体:

  1. struct thread_info {
  2.     struct task_struct  *task;
  3.     struct exec_domain  *exec_domain;
  4.     unsigned long       flags;
  5.     __u32           status;
  6.     __u32           cpu;
  7.     int         preempt_count;
  8.     mm_segment_t        addr_limit;
  9.     struct restart_block    restart_block;
  10.     void __user     *sysenter_return;
  11.     unsigned long           previous_esp;
  12.     __u8            supervisor_stack[0];
  13. };
注意上面结构体的最后一个字段,数组长度居然为0。对于C语言,无论是理论还是实践经验来说,我还算是比较熟悉的。不过,说实话,这个用法我还是第一次遇到,不知有何妙用。出于对技术的好奇,我查阅了一些资料,发现只有GNU C允许使用这种用法,目的是为了访问不定长结构体时节省空间和便利性。我用下面这个例子来说明。
  1. struct demo {
  2.     int     a;
  3.     char    b[256];
  4.     char    follow[0];
  5. };
假如,现在程序中要分配一个struct demo结构体,并紧邻其后分配长度为LEN个字节空间,则可以使用如下方法得到:
struct demo *demo = (struct demo *) malloc (sizeof(strcut demo) + LEN);
这样我们就可以用 demo->follow 来访问结构体demo随后的空间数据,非常方便。当然,我们可以使用指针来达到这样的目的。
  1. struct demo {
  2.     int     a;
  3.     char    b[256];
  4.     char    *follow;
  5. };
  6. struct demo *demo = (struct demo *) malloc (sizeof(strcut demo) + LEN);
同样可以达到零长度数组的效果,但是却多分配了一个char指针。如果分配额外数据空间还好,否则就是白白浪费了空间。不过,很可惜,只有GNU C支持这种用法,其他C编译器不支持。不过,我想也很少有人去这么用的,权当是长长见识吧。
(Aiguille.LIU/刘爱贵, aigui.liu@gmail.com)

抱歉!评论已关闭.