What is the purpose of a zero length array in a struct? [duplicate]

When I'm looking the Linux kernel code, found the below code:

   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];
};

Notice that the last variable "supervisor_stack", it is a zero length array, what is the usage of it? Thanks in advance!


Solution 1:

It's the pre-C99 version of a flexible array member, offered by GCC as an extension.

The C99 way is to define the flexible array member with empty brackets,

__u8  supervisor_stack[];

It's used to store data whose amount is not constant contiguous to the struct. Memory is allocated in the form

struct foo *ptr = malloc(sizeof *ptr + whatever_is_needed);

In paragraph 18 of 6.7.2.1, the standard (draft N1570) describes them:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

Solution 2:

It's a common C hack to declare what can be called a variable length-array (where you define the size at allocation time

Example:

struct line {
   int length;
   char contents[0];
 };

 struct line *thisline = (struct line *)
   malloc (sizeof (struct line) + this_length);
 thisline->length = this_length;

This way you have a structure definition of your data, which also stores the array length for obvious convecience purposes, but you're not constrained by the fixed size that is usually associated with a struct

Example taken from here (also more info in there)