c - Unable to malloc string in struct -


my struct looks this:

struct tree{     char *name;     int num_subdirs;     struct tree **subdirs; } 

i receiving buffer contains entire tree serialized in buffer. trying deserialize in function:

struct tree *t; //buffer filled, received somewhere else. int res = deserialize(t, buf); //call function deserialize  //deserialize function             //buf = {../,2}{sd,0}{|}{sr,1}{sk,0}{|}{|}    │406     int dfsdeserialize(struct tree *dt, void *buf, int *q){                                                                                                                       │    │407         char name[maxpathlen];                                                                                                                                                           │    │408         char delim[3];                                                                                                                                                                   │    │409         int len, numsubs, i;                                                                                                                                                             │                                                                                                                                                         │    │411         sscanf(buf+(*q),"%3s",delim);                                                                                                                                                    │    │412         if(!strcmp(delim,"{|}")){                                                                                                                                                        │    │413             (*q)+=3;                                                                                                                                                                     │    │414             return 1;                                                                                                                                                                    │    │415         }                                                                                                                                                                                │    │416         sscanf((buf + (*q)), "{%[^,],%d}%n", name, &numsubs, &len);                                                                                                                      │                                                                                                                                          │   >│419         int slen = strlen(name);                                                                                                                                                         │    │420         dt->name = calloc(slen + 1, 1);                                                                                                                                                  │    │421         dt->subdirs = malloc(numsubs*sizeof(struct tree *));                                                                                                                      │    │422         strcpy(dt->name, name);                                                                                                                                                          │    │423         dt->num_subdirs = numsubs;                                                                                                                                                       │    │424         (*q)+=len;                                                                                                                                                                       │    │425         for(i = 0; i< numsubs; i++){                                                                                                                                                     │    │426             dt->subdirs[i] = malloc(sizeof(struct tree));                                                                                                                         │    │427             dfsdeserialize(dt->subdirs[i], buf, q);                                                                                                                                      │    │428         }                                                                                                                                                                                │    │429         return 0;                                                                                                                                                                        │    │430     }                                                                                                                                                                                  │ 

i have tried several different ways of allocating memory string fails every single time! don't know why t->name 0x0. please help.

i think culprit this

t = malloc(sizeof(sizeof tree)); 

you meant

t = malloc(sizeof(struct tree)); 

you can use more handier strdup copy string on heap

t->name = strdup(name); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -