I am writing a server program which has a function to pack a struct to a protocol-confining struct. The function looks like this:
lpacket pack_interp_ilpacket(ilpacket* pck)
{
lpacket* a = malloc(pck->size);
pack_lpack_data(pck->size, pck->msgtype, pck->following, a);
return *a;
}
Now the problem is, when I call malloc on the lpacket and dereference it to make it return a value (and not a reference), I am no longer able to free that memory. If I call free at the place where the value is returned, I only free the stack-allocated struct. And a call to free AFTER the return statement wouldn't make much sense...
Any ideas?
lpackethas a flexible array member, is that right? And to answer you question in-comment, that is exactly what that means. you're leaking memory in three short lines (2 shy of the record). Do you own this function, and is it some callback and therefore an unmodifiable signature? - WhozCraiglpacketis declared. - alk