我有一个客户端进程,它构建一个IPC message struct queue_msg,该消息结构将通过linux IPC msg队列发送到服务器。大小为68字节。struct定义为:
struct FOO_TYPE {
long mtype;
struct {
int sev;
char msg[32];
char bt[32];
} foomsg;
};客户端在函数中本地声明一个指向FOO_TYPE的a结构的指针,并为其分配错误的空间。然后,代码加载sev、msg和bt字段。
static struct FOO_TYPE *FooEntry = NULL;...code已裁剪
if (FooEntry == NULL)
FooEntry = malloc(sizeof(struct FOO_TYPE));
memset(FooEntry, 0, sizeof(struct FOO_TYPE));...code已裁剪
MsgSize = sizeof(FooEntry) - sizeof(long);
FooEntry->mtype = CHANGESTATUS;
FooEntry->foomsg.sev = serr->serr_data->sev;
strcpy(FooEntry->syserrmsg.emsg, elog);
strcpy(FooEntry->syserrmsg.bt, btlog);..。代码裁剪
result = msg_snd(FooExchange, FooEntry, MsgSize, IPC_WAIT);接收IPC消息的服务器正在获取68个字节(例如: sizeof FOO_TYPE),然而,其中的字段间歇性地丢失或成为垃圾。
我是否必须在结构中为结构中的字段也分配空格??
发布于 2021-04-14 21:51:50
这至少是一个错误:
MsgSize = sizeof(FooEntry) - sizeof(long);FooEntry是这里定义的指针:
static struct FOO_TYPE *FooEntry = NULL;所以sizeof(FooEntry)给出了一个指针的大小,而不是一个结构的大小。
你可能想要
MsgSize = sizeof(*FooEntry) - sizeof(long);或者仅仅是
MsgSize = sizeof(FooEntry->foomsg);https://stackoverflow.com/questions/67092709
复制相似问题