0

I'm going through the kernel linux source code reading just to improve my knowledge of it.

I just stepped into the declaration of a structure where there's some strange "just one element arrays".

I'm trying to figuring out why use one element array and not the istance of the type.

Here the code I'm referring to

struct files_struct {
  /*
   * read mostly part
   */
    atomic_t count;
    bool resize_in_progress;
    wait_queue_head_t resize_wait;

    struct fdtable __rcu *fdt;
    struct fdtable fdtab;
  /*
   * written part on a separate cache line in SMP
   */
    spinlock_t file_lock ____cacheline_aligned_in_smp;
    unsigned int next_fd;
    unsigned long close_on_exec_init[1];
    unsigned long open_fds_init[1];
    unsigned long full_fds_bits_init[1];
    struct file __rcu * fd_array[NR_OPEN_DEFAULT];
};

For example why in place of unsigned long close_on_exec_init[1]; they do not just declared unsigned long close_on_exec_init;.

Alessandro
  • 598
  • 1
  • 7
  • 18
  • Better look at how these members are *used*. – Some programmer dude Aug 08 '19 at 14:00
  • 1
    See also https://stackoverflow.com/q/24028874/10152624. It seems to be about the same struct from the same file. – salcc Aug 08 '19 at 14:04
  • @salcc Thanks for your suggestions. At least your first suggestion does not fit my scenario. If close_on_exec_init were a flexible array, the followings symbols would become meaningless because they would fall into the close_on_exec_init augmented array. Your second suggestion is more relevant, but I couldn't find a compelling answer to my question. Neither the author of the original question did. Not use & when you need the address is convenient, but have to type [0] when the value is needed completely cancels out the benefit. – Alessandro Aug 08 '19 at 14:40
  • 1
    A one element array allows code, from a caller's point-of-view, to pass the object by reference. Unknown it that is the design goal here though. – chux - Reinstate Monica Aug 08 '19 at 14:43

0 Answers0