Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux

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!

like image 778
Derui Si Avatar asked Jul 31 '12 05:07

Derui Si


People also ask

What is a zero-length array?

Zero-length arrays, also known as flexible arrays, are used to implement variable-length arrays, primarily in structures. That's a little confusing, so let's look at an example. Say you wanted a structure to represent an email: Yes, we're missing a lot of fields. This is just an example; bear with me.

Why do some structures end with an array of size 1?

Some Windows structures are variable-sized, beginning with a fixed header, followed by a variable-sized array. When these structures are declared, they often declare an array of size 1 where the variable-sized array should be.

Can we create array of length zero?

Yes, you can create arrays of any type with length zero.


2 Answers

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.

like image 158
Daniel Fischer Avatar answered Oct 07 '22 23:10

Daniel Fischer


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)

like image 45
Naps62 Avatar answered Oct 08 '22 01:10

Naps62