Does a flexible array member increase sizeof a struct?

后端 未结 2 865
陌清茗
陌清茗 2020-12-10 15:59

I have the following kind of code:

typedef struct
{    
    u32 count;
    u16 list[];   
} message_t;
...

message_t* msg = (message_t*)buffer;  
msg->co         


        
相关标签:
2条回答
  • 2020-12-10 16:29

    Here's what the standard says:

    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.

    0 讨论(0)
  • 2020-12-10 16:49

    Your example do work since C has not arrays that dynamically become bigger when you add elements. So size of *msg is sizeof u32 + paddings, if any, but it won't count for list member, which you have to consider by yourself when you "alloc" the buffer and when you want to know the actual size of that "object", as you did.

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