小编mas*_*key的帖子

如何释放一个char指针数组?

我使用此方法将列表中的值转换为数组,以便在execvp()中使用 - 系统调用:

char **list2argarray(struct shellvalue *values, int count)
{
    char **array = (char **)malloc((count + 1) * sizeof(char *));
    int i = 0;

    while (values)
    {
        char *word = values->word;

        array[i] = (char *)malloc(sizeof(word) + 1);
        strcpy(array[i], word);
        values = values->next;
        i++;
    }
    array[i] = NULL;
    return array;
}
Run Code Online (Sandbox Code Playgroud)

什么是释放这种阵列的正确方法?我试过像这样的东西

void freeargpointer(char **array, int count)
{
    int i = 0;

    while (*array)
    {
        free(*array);
        (*array)++;
    }
}
Run Code Online (Sandbox Code Playgroud)

但每当我到达free-syscall时,在调试时,程序崩溃时会出现如下错误:

free():下一个大小无效(快):0x000000000060c180****

c arrays malloc free pointers

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

arrays ×1

c ×1

free ×1

malloc ×1

pointers ×1