Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9220369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:19:49+00:00 2026-06-18T03:19:49+00:00

In the Linux kernel code I found the following thing which I can not

  • 0

In the Linux kernel code I found the following thing which I can not understand.

 struct bts_action {
         u16 type;
         u16 size;
         u8 data[0];
 } __attribute__ ((packed));

The code is here: http://lxr.free-electrons.com/source/include/linux/ti_wilink_st.h

What’s the need and purpose of an array of data with zero elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:19:51+00:00Added an answer on June 18, 2026 at 3:19 am

    This is a way to have variable sizes of data, without having to call malloc (kmalloc in this case) twice. You would use it like this:

    struct bts_action *var = kmalloc(sizeof(*var) + extra, GFP_KERNEL);
    

    This used to be not standard and was considered a hack (as Aniket said), but it was standardized in C99. The standard format for it now is:

    struct bts_action {
         u16 type;
         u16 size;
         u8 data[];
    } __attribute__ ((packed)); /* Note: the __attribute__ is irrelevant here */
    

    Note that you don’t mention any size for the data field. Note also that this special variable can only come at the end of the struct.


    In C99, this matter is explained in 6.7.2.1.16 (emphasis mine):

    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.

    Or in other words, if you have:

    struct something
    {
        /* other variables */
        char data[];
    }
    
    struct something *var = malloc(sizeof(*var) + extra);
    

    You can access var->data with indices in [0, extra). Note that sizeof(struct something) will only give the size accounting for the other variables, i.e. gives data a size of 0.


    It may be interesting also to note how the standard actually gives examples of mallocing such a construct (6.7.2.1.17):

    struct s { int n; double d[]; };
    
    int m = /* some value */;
    struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));
    

    Another interesting note by the standard in the same location is (emphasis mine):

    assuming that the call to malloc succeeds, the object pointed to by p behaves, for most purposes, as if p had been declared as:

    struct { int n; double d[m]; } *p;
    

    (there are circumstances in which this equivalence is broken; in particular, the offsets of member d might not be the same).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm looking the Linux kernel code, found the below code: struct thread_info {
typedef void (*work_func_t)(struct work_struct *work); I found above typedef in Linux kernel source code,
I found this code in linux kernel (arch/x86/boot/pmjump.S) # Set up TR to make
Where exactly can I find in the Linux kernel code the limit set for
i found this in linux kernel code http://gitorious.org/pandroid/kernel-omap/blobs/5ed7607d45b300a37dd13ad1c79adea56f6687ce/arch/arm/mach-omap2/board-omap4panda.c MACHINE_START(OMAP4_PANDA, OMAP4430 Panda Board) .phys_io =
I am reading the Linux kernel code for copy_fom_user, which is architecture dependent and
Browsing the Linux kernel sources I found some piece of code where a block
I'm reading linux kernel code and I encounter something like the following: typedef void
I have this code in linux kernel: #define task_cred_xxx(task, xxx) ({ __typeof__(((struct cred *)NULL)->xxx)
In the Linux kernel source code I found this function: static int __init clk_disable_unused(void)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.