灵活的数组成员是否会增加结构的大小?

前端之家收集整理的这篇文章主要介绍了灵活的数组成员是否会增加结构的大小?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下类型的代码
typedef struct
{    
    u32 count;
    u16 list[];   
} message_t;
...

message_t* msg = (message_t*)buffer;  
msg->count = 2;
msg->list[0] = 123;
msg->list[1] = 456;

size_t total_size = sizeof(*msg) + sizeof(msg->list[0]) * msg->count;  

send_msg( msg,total_size );

有问题的线是sizeofs的线.我不确定这是计算所需空间的正确方法.
sizeof(* msg)是否包含有关列表成员的内容

我可以用我的编译器测试它,但是在这种情况下每个编译器的工作方式是否相似?

解决方法

这是标准所说的:

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.

猜你在找的C&C++相关文章