Does a flexible array member increase sizeof a struct?

后端 未结 2 868
陌清茗
陌清茗 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: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.

提交回复
热议问题