I have the following kind of code:
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 );
Problematic line is the line with sizeofs. I am not sure is that correct way to count needed space.
Does sizeof(*msg)
contains already something about the list
member?
I can test it with my compiler, but does every compiler work similary in this case?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…