c - Malloc struct with double pointer -
i have structure looks this:
typedef struct{ char* name; int count; node **subnodes; }node;
i receive name , count on network 1 one whole k-ary tree , reconstruct tree. how malloc struct?
node *n = null; fun(n, buf); //call function fun void fun(node *n, void *buf){ //successfully extracted name , count buf // count 5, i.e. node should have 5 subnodes , // name root n = malloc(sizeof(*n)+strlen(name)); n->name = name; n->count = count; for(int i=0; i<n->count;i++) fun(n->subnodes[i], buf+some_increment); }
this crashes call fun second time. how should malloc properly? should malloc-ing each subnode?
first off, realise leaky? n
on stack - aren't returning or "saving" value give anywhere permanently.
then haven't allocated memory subnodes
, indexing reading unallocated memory.
without more complete example hard further, @ minimum need malloc subnodes
.
Comments
Post a Comment