I am passing the address of a structure to the function.
void validate(void *ptr)
{
// A variable of type Msgmt Structure
Msgmt msg;
memset(&msg, 0, MSG_SIZE);
// Type casting the void pointer to this structure dev_t
dev_t *elem = (dev_t*)ptr;
msg.msg_id = 10;
if (elem->devCategory == VAL){
// This statement crashes the code
// memcpy(msg.messageData.value.data, elem->data, LEN);
// This statement goes through
memcpy(&msg.messageData.value.data,elem->data,LEN) ;
}
// ... do something
}
messageData is a union within struct msg and value is a member of union and data is an array of unsigned char.
how should I do a memcpy of elem->data
to the msg.messageData.value.data
? ( elem->data
is an array of unsigned char
)
thanks !!!
The statement crashes, because the pointer is NULL.
Here you clear the members.
memset(&msg, 0, MSG_SIZE);
// Type casting the void pointer to this structure dev_t
dev_t *elem = (dev_t*)ptr;
Here is the only thing that you initiliaze something.
msg.msg_id = 10;
if (elem->devCategory == VAL){
And here you access the NULL pointer, which luckily for you crashes.
// This statement crashes the code
// memcpy(msg.messageData.value.data, elem->data, LEN);
And here you will corrupt other memory most likely, without crashing.
// This statement goes through
memcpy(&msg.messageData.value.data,elem->data,LEN) ;
}
// ... do something
}
You must assign msg.messageData.value.data
some defined memory, before you can use it as a target for memcp
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.