Why does such a struct contain two array fields containing only one element?

前端 未结 2 1708
青春惊慌失措
青春惊慌失措 2021-01-11 19:17

Please Note: This question is not a duplicate of ( One element array in struct )

The following code is excerpted from the Linux kernel source (versi

相关标签:
2条回答
  • 2021-01-11 20:04

    These fields are an optimization so Linux doesn't have to perform as many allocations for a typical process that has no more than BITS_PER_LONG open file descriptors.

    The close_on_exec_init field provides the initial storage for fdt->close_on_exec when a files_struct is allocated. (See dup_fd in fs/file.c.)

    Each bit of fdt->close_on_exec is set if the corresponding file descriptor has the “close-on-exec” flag set. Thus Linux only needs to allocate additional space for fdt->close_on_exec if the process has more open file descriptors than the number of bits in an unsigned long.

    The open_fds_init field serves the same function for the fdt->open_fds field. The fd_array field serves the same function for the fdt->fd field. (Note that fd_array has a size of BITS_PER_LONG.)

    The close_on_exec_init and open_fds_init fields formerly had type struct embedded_fd_set, but were changed to bare arrays in this commit. The commit message doesn't explain why the author chose to use one-element arrays instead of bare scalars. Perhaps the author (David Howells) simply wanted to avoid using the & operator.

    0 讨论(0)
  • 2021-01-11 20:18

    My best guess: The addresses of these fields are used much more often than their actual values. In this case, making them size-1 arrays saves typing & every time their address is needed, since in C using the name of an array in an expression is in nearly all cases exactly equivalent to taking the address of its first element:

    int x;
    int y[1];
    
    function_that_needs_address_of_int(&x);
    function_that_needs_address_of_int(y);
    function_that_needs_address_of_int(&y[0]);    // Identical to previous line
    

    (As others have pointed out in the comments, it can't be that the fields are being used as a hack for variable-length arrays, since there is more than one and they don't appear at the end of the struct.)

    [EDIT: As pointed out by user3477950, an array name is not always identical to the address of its first element -- in certain contexts, like the argument to sizeof, they mean different things. (That's the only context I can think of for C; in C++, passing an array name as an argument can also enable a template parameter's type to be inferred to be a reference type.)]

    0 讨论(0)
提交回复
热议问题