收到以下警告:使用灵活数组成员传递结构的 ABI 在 GCC 4.4 中已更改
当我尝试运行我的程序时,我收到此警告和一些奇怪的错误。 以下结构中的rmi_pdu
包含我想要访问的可变大小的数组。
struct rmi_message_s { /* Queue element containing Rmi message */
struct rmi_message_s *hnext;
struct rmi_message_s *hprev;
uint16_t gen_counter; /* Generation counter */
time_value send_time;
uint8_t retry_count;
TAILQ_ENTRY(rmi_message_s) rmi_message_next;
rmi_message_pdu rmi_pdu; /* contains a variable sized array */
};
typedef struct {
uint16_t zero;
uint16_t type;
uint8_t version;
uint8_t len;
uint8_t protocol;
uint16_t edge_port;
uint16_t core_port;
uint32_t connexus_id;
pi_ipv4_addr_t edge_addr;
pi_ipv4_addr_t core_addr;
uint16_t gen_count; /* Integer to identify a stale packet */
uint8_t payload[];
} rmi_message_pdu;
问题是当我尝试释放动态分配的内存时。内容在那里,但 free()
API 正在 abort()
ing 。这就是核心的样子
in raise () from /lib64/libc.so.6
in abort () from /lib64/libc.so.6
in __libc_message () from /lib64/libc.so.6
in _int_free () from /lib64/libc.so.6
in free () from /lib64/libc.so.6
in free (p=0x2aaabc000fa0) at mallocdbg.cc:188
in rmi_hash_cleanup (rmi_msg=0x2aaabc000fa0) at tcpsvc_rmi.c:126
in rmi_process_response (response_packet=0x27422e00) at tcpsvc_rmi.c:239
in rmi_message_handle (pkt=0x27422e00 "", cnt=28) at tcpsvc_base.c:154
in udpif_worker (arg=0x2b01f7014340) at rumpnet_virtif/if_udp_netbsd_guest.c:573
in threadbouncer (arg=0x2b01f7016428) at rumpkern/emul.c:428
in clone () from /lib64/libc.so.6
这就是分配的样子。想要使用 rmi 的调用者将传递大小作为参数。
struct rmi_message_s *rmi_msg;
rmi_msg = (struct rmi_message_s *) malloc (sizeof(struct rmi_message_s *) + len * sizeof(uint8_t));
len
作为参数传递。
When I try and run my program I get this warning and some weird bugs.rmi_pdu
in the following structure contains a variable sized array which I want to access.
struct rmi_message_s { /* Queue element containing Rmi message */
struct rmi_message_s *hnext;
struct rmi_message_s *hprev;
uint16_t gen_counter; /* Generation counter */
time_value send_time;
uint8_t retry_count;
TAILQ_ENTRY(rmi_message_s) rmi_message_next;
rmi_message_pdu rmi_pdu; /* contains a variable sized array */
};
typedef struct {
uint16_t zero;
uint16_t type;
uint8_t version;
uint8_t len;
uint8_t protocol;
uint16_t edge_port;
uint16_t core_port;
uint32_t connexus_id;
pi_ipv4_addr_t edge_addr;
pi_ipv4_addr_t core_addr;
uint16_t gen_count; /* Integer to identify a stale packet */
uint8_t payload[];
} rmi_message_pdu;
the problem is when I am trying to free the memory which I am dynamically allocating. The contents are there but the free()
API is abort()
ing . This what the core looks like
in raise () from /lib64/libc.so.6
in abort () from /lib64/libc.so.6
in __libc_message () from /lib64/libc.so.6
in _int_free () from /lib64/libc.so.6
in free () from /lib64/libc.so.6
in free (p=0x2aaabc000fa0) at mallocdbg.cc:188
in rmi_hash_cleanup (rmi_msg=0x2aaabc000fa0) at tcpsvc_rmi.c:126
in rmi_process_response (response_packet=0x27422e00) at tcpsvc_rmi.c:239
in rmi_message_handle (pkt=0x27422e00 "", cnt=28) at tcpsvc_base.c:154
in udpif_worker (arg=0x2b01f7014340) at rumpnet_virtif/if_udp_netbsd_guest.c:573
in threadbouncer (arg=0x2b01f7016428) at rumpkern/emul.c:428
in clone () from /lib64/libc.so.6
This is what the allocation looks like. The caller who wants to use rmi, will pass the size as an argument.
struct rmi_message_s *rmi_msg;
rmi_msg = (struct rmi_message_s *) malloc (sizeof(struct rmi_message_s *) + len * sizeof(uint8_t));
len
is passed as an argument.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有分配足够的内存:
您有
...sizeof(struct rmi_message_s *)...
,但它应该是...sizeof(struct rmi_message_s)...
代码>You are not allocating enough memory:
You had
...sizeof(struct rmi_message_s *)...
, but it should have been...sizeof(struct rmi_message_s)...
您几乎肯定不想按值传递此对象。而是传递对象的指针或引用。
该警告是因为,如果您混合 GCC 4.3 或更早版本和 GCC 4.4 或更高版本的代码,它们在如何在堆栈上传递该结构方面是不兼容的。无论如何,我很确定你实际上并不想将其传递到堆栈上。这将是非常低效的,并且你会失去你的有效负载。
You almost certainly don't want to pass this object by value. Pass a pointer or reference to the object instead.
The warning is because if you are mixing code from GCC 4.3 or earlier, and GCC 4.4 or newer, they are incompatible in regards to how they would pass that struct on the stack. At any rate, I'm pretty sure you don't actually want to pass that on the stack anyway. It'd be hugely inefficient, and you'd lose your payload.