The reason why it is crashing is that all messages have a few builtin
members that are usually initialized via ITEM__INIT. If you look at
the generated Item it looks like:
struct _Item
{
ProtobufCMessage base_message;
uint32_t id;
};
The best way to construct a message is to use C's assignment
operator. Rewriting your code strictly:
LookupReply nlr = LOOKUP_REPLY__INIT;
Item item = ITEM__INIT;
(&nlr)->replica_list = (Item**)malloc(1*sizeof(Item*));
(&nlr)->n_replica_list = 1;
Item* item1 = (Item*)malloc(sizeof(Item));
item.id = 1;
*item1 = item;
(&nlr)->replica_list[0] = item1;
int len = lookup_reply__get_packed_size(nlr);
But that's kind of overkill in a way, it's shorter just to write:
LookupReply nlr = LOOKUP_REPLY__INIT;
Item item1 = ITEM__INIT;
Item *items[1] = { &item };
nlr.n_replica_list = 1;
nlr.replica_list = items;
item1.id = 1;
int len = lookup_reply__get_packed_size(&nlr);
Furthermore, using the latter approach, you can avoid malloc(), and
therefore (most of) the possibility of memory leaks.