This question already has an answer here:
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!
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,
It's used to store data whose amount is not constant contiguous to the struct. Memory is allocated in the form
In paragraph 18 of 6.7.2.1, the standard (draft N1570) describes them:
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:
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)