GNU C中的零长度数组

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

struct thread_info {
    struct task_struct  *task;
    struct exec_domain  *exec_domain;
    unsigned long       flags;
    __u32           status;
    __u32           cpu;
    int         preempt_count;
    mm_segment_t        addr_limit;
    struct restart_block    restart_block;
    void __user     *sysenter_return;
    unsigned long           previous_esp;
    __u8            supervisor_stack[0];
};


注意上面结构体的最后一个字段,数组长度居然为0。对于C语言,无论是理论还是实践经验来说,我还算是比较熟悉的。不过,说实话,这个用法我还是第一次遇到,不知有何妙用。出于对技术的好奇,我查阅了一些资料,发现只有GNU C允许使用这种用法,目的是为了访问不定长结构体时节省空间和便利性。我用下面这个例子来说明。

struct demo {
    int     a;
    char    b[256];
    char    follow[0];
};


假如,现在程序中要分配一个struct demo结构体,并紧邻其后分配长度为LEN个字节空间,则可以使用如下方法得到:

struct demo *demo = (struct demo *) malloc (sizeof(strcut demo) + LEN);

这样我们就可以用 demo->follow 来访问结构体demo随后的空间数据,非常方便。当然,我们可以使用指针来达到这样的目的。

struct demo {
    int     a;
    char    b[256];
    char    *follow;
};
struct demo *demo = (struct demo *) malloc (sizeof(strcut demo) + LEN);


同样可以达到零长度数组的效果,但是却多分配了一个char指针。如果分配额外数据空间还好,否则就是白白浪费了空间。不过,很可惜,只有GNU C支持这种用法,其他C编译器不支持。不过,我想也很少有人去这么用的,权当是长长见识吧。


原文地址:http://blog.csdn.net/liuaigui/article/details/3680404

你可能感兴趣的:(linux,C语言,结构体,C数组)