Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在结构中删除malloc字符串_C_String_Struct_Malloc - Fatal编程技术网

无法在结构中删除malloc字符串

无法在结构中删除malloc字符串,c,string,struct,malloc,C,String,Struct,Malloc,我的结构如下所示: struct tree{ char *name; int num_subdirs; struct tree **subdirs; } 我正在接收一个缓冲区,其中包含在缓冲区中序列化的整个树。我试图在此函数中反序列化它: struct tree *t; //buffer is filled, received from somewhere else. int res = deserialize(t, buf); //call function dese

我的结构如下所示:

struct tree{
    char *name;
    int num_subdirs;
    struct tree **subdirs;
}
我正在接收一个缓冲区,其中包含在缓冲区中序列化的整个树。我试图在此函数中反序列化它:

struct tree *t;
//buffer is filled, received from 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     }   

                                                                                                                                                                             │
结构树*t;
//缓冲区已填充,从其他地方接收。
int res=反序列化(t,buf)//调用函数反序列化
//反序列化函数
//buf={../,2}{sd,0}{{sr,1}{sk,0}{sk}{
│406 int-dfs反序列化(结构树*dt,void*buf,int*q){│
│407字符名[MAXPATHLEN];│
│408 char delim[3];│
│北马里亚纳州努姆苏伯斯国际酒店409号;│
│
│411sscanf(buf+(*q),“%3s”,delim);│
│412如果(!strcmp(delim,{}”){│
│413(*q)+=3;│
│414返回1;│
│415         }                                                                                                                                                                                │
│416 sscanf((buf+(*q)),“{%[^,],%d}%n”,name,&numsubs,&len);│                                                                                                                                          │
>│419 int slen=strlen(名称);│
│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表示(i=0;isubdirs[i]=malloc(sizeof(struct tree));│
│427 dfs反序列化(dt->subdirs[i],buf,q);│
│428         }                                                                                                                                                                                │
│429返回0;│
│430     }   
│

我尝试了几种为字符串分配内存的方法,但每次都失败了!我不知道为什么t->name总是0x0。请帮忙

我认为罪魁祸首是这个

t = malloc(sizeof(sizeof tree));
你可能是说

t = malloc(sizeof(struct tree));
您还可以使用更方便的strdup在堆上复制字符串

t->name = strdup(name);

我认为罪魁祸首是这个

t = malloc(sizeof(sizeof tree));
你可能是说

t = malloc(sizeof(struct tree));
您还可以使用更方便的strdup在堆上复制字符串

t->name = strdup(name);
C使用“传递值”。将参数传递给函数时,函数将收到该参数的副本。因此,当你有:

struct tree *t;
int res = deserialize(t, buf); //call function deserialize
该函数所做的任何操作都不会影响
t
。该函数获取当前值
t
的副本。(由于
t
未初始化,这可能会导致未定义的行为)

相反,您需要告诉函数在内存中的
t
位置:

int res = deserialize(&t, buf);
该函数可能如下所示:

int deserialize(struct tree **p_t, void *buf)
{
    struct tree *t = malloc(sizeof *t);
    // ... set up t as before

    // tell the caller about what we did
    *p_t = t;
}

替代方案