Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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
C strdup之后如何释放()?_C - Fatal编程技术网

C strdup之后如何释放()?

C strdup之后如何释放()?,c,C,当调用如下所示时,如何从strdup中free():*(result+idx++)=strdup(token) 更多背景: char **str_split(char *a[], char *a_str, const char a_delim) { char **result = 0; size_t count = 0; char *tmp = a_str; char *last_comma = 0; char delim[2]; delim[0]

当调用如下所示时,如何从
strdup
free()
*(result+idx++)=strdup(token)

更多背景:

char **str_split(char *a[], char *a_str, const char a_delim) {
    char **result = 0;
    size_t count = 0;
    char *tmp = a_str;
    char *last_comma = 0;
    char delim[2];
    delim[0] = a_delim;
    delim[1] = 0;

    /* Count how many elements will be extracted. */
    while (*tmp) {
        if (a_delim == *tmp) {
            count++;
            last_comma = tmp;
        }
        tmp++;
    }

    /* Add space for trailing token. */
    count += last_comma < (a_str + strlen(a_str) - 1);

    /* Add space for terminating null string so caller
       knows where the list of returned strings ends. */
    count++;

    result = malloc(sizeof(char *) * count);
    if (result == NULL) {
        printf("Error allocating memory!\n"); //print an error message
        return result; //return with failure
    }

    if (result) {
        size_t idx = 0;
        char *token = strtok(a_str, delim);

        while (token) {
            assert(idx < count);
            *(result + idx++) = strdup(token); /* memory leak! how to free() */
            token = strtok(0, delim);
        }
        assert(idx == count - 1);
        *(result + idx) = 0;
    }

    return result;
}
char**str_split(char*a[],char*a_str,const char a_delim){
字符**结果=0;
大小\u t计数=0;
char*tmp=a_str;
char*last_逗号=0;
char-delim[2];
delim[0]=a_delim;
delim[1]=0;
/*计算将提取多少元素*/
而(*tmp){
如果(a_delim==*tmp){
计数++;
最后一个逗号=tmp;
}
tmp++;
}
/*为尾部标记添加空间*/
计数+=最后一个逗号<(a_str+strlen(a_str)-1);
/*为终止空字符串添加空间,以便调用方
知道返回字符串列表的结束位置*/
计数++;
结果=malloc(sizeof(char*)*计数);
如果(结果==NULL){
printf(“错误分配内存!\n”);//打印错误消息
返回结果;//返回失败
}
如果(结果){
大小\u t idx=0;
char*token=strtok(a_str,delim);
while(令牌){
断言(idxThe
*(result+idx)=0行可以判断序列的结束位置。
只需
free()
完成使用后分配的所有元素。 此外,要存储自身的数组在使用完毕后应为
free()
d

char ** ret = char **str_split(/* some arguments */);
size_t idx;

/* deal with the result */

for (idx = 0; *(ret + idx) != NULL; idx++) {
    free(*(ret + idx));
}
free(ret);

当使用
result
时,调用此函数的代码必须
释放
result
中的每个指针,然后
释放
结果本身。

结果[idx++]
我认为太容易理解了?
delim[0]
delim[1]
,因此,数组索引运算符不应被禁止。调用此函数的代码在使用完后,需要对所有存储的指针调用
free
them@MikeCAT:实际上,索引运算符作用于指针,而不是数组。@Olaf说的是“6.5.2.1数组下标”,而不是“指针下标”使用
ret[idx]
的风格比
*(ret+idx)
更好,现在它可以工作了。我的案例看起来像(idx=0;*(dealloc[f]+idx)!=NULL;idx++){
,因为我有一个数组。