微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有以下类型的代码
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.

原文地址:https://www.jb51.cc/c/111313.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐