c - 为三维数组动态分配内存

标签 c arrays malloc realloc

我不明白。几个小时以来,我一直在寻找以下功能,但不明白为什么最后遍历数组时会出现内存错误。如果我不遍历它,它运行时不会出错。该函数应该首先用“|”分割输入字符串然后通过“”,将所有内容放入一个 3 维数组中。例如像“ls -l | grep test”这样的字符串应该在第一维的每个命令和第二维的每个参数中拆分。第三个字符串。

我认为错误在于动态分配内存。但我不明白。

char ***splitInput(char *input){

    // Here is splitted data stored (explanation in main)
    char ***commands;

    char *argT=NULL;
    char *commT=NULL;
    char *commTcopy=NULL;
    char *save_commTcopy;
    char *save_input;


    // First filter by | and new line to get commands
    commT = strtok_r(input,"|\n",&save_input);
    int c = 0;
    while(commT != NULL){

        // alloc memory for commands
        //  -> number of commands (c1+1)
        //  -> +1 for NULL command in the end
        commands = realloc(commands,sizeof(*commands)*(c+1+1));
        commTcopy = realloc(commTcopy,sizeof(*commTcopy)*strlen(commT)+1);

        // Make Copy of command
        strcpy(commTcopy,commT);
        printf("Cutting command %s\n",commT);

        // Then filter by space and new line to get arguments of command
        argT=strtok_r(commTcopy," \n",&save_commTcopy);

        char **command;
        // command pointer for easier handling
        command = *(commands+c);
        int c1 = 0;
        while(argT != NULL){
            // alloc memory for arguments
            //  -> number of commands (c1+1)
            //  -> +1 for NULL argument in the end
            command = realloc(command,sizeof(*command)*(c1+1+1));
            // alloc memory for argument string
            *(command+c1) = realloc(*(command+c1),sizeof(**command)*strlen(argT)+1);
            // Copy argument to commands three dimensional array
            strcpy(*(command+c1),argT);
            printf("-- %s\n",argT);
            // next argument
            argT = strtok_r (NULL, " \n",&save_commTcopy);
            c1++;
        }
        *(command+c1)=NULL;
        *(commands+c)=command;

        // get next command
        commT = strtok_r (NULL, "|\n",&save_input);
        c++;
    }
    *(commands+c)=NULL;

    // Walk through array for proof
    /*int c2=0;
    while(*(commands+c2)!=NULL){
        printf("Proof Command %d\n",c2);fflush(stdin);
        char **command;
        command = *(commands+c2);
        int c1=0;
        while(*(command+c1)!=NULL){
            printf("--- %s\n",*(command+c1));fflush(stdin);
            c1++;
        }
        c2++;
    }*/

    return commands;
}

最佳答案

我没有仔细阅读您的代码。但我至少发现了一个问题。

您正在使用 realloc 来分配和调整内存大小。 realloc 函数通常用于调整内存大小。但是如果你传递一个空指针给它,它会直接分配内存。

您的代码中的问题在这里:

    //  -> +1 for NULL command in the end
    commands = realloc(commands,sizeof(*commands)*(c+1+1));
    commTcopy = realloc(commTcopy,sizeof(*commTcopy)*strlen(commT)+1);

    //...

    char **command;
    // command pointer for easier handling
    command = *(commands+c);
    int c1 = 0;
    while(argT != NULL){
        // alloc memory for arguments
        //  -> number of commands (c1+1)
        //  -> +1 for NULL argument in the end
        command = realloc(command,sizeof(*command)*(c1+1+1));

在每一轮迭代中,您将 commands 重新分配给一个更大的数组,这很好。但问题是分配的内存单元 *(commands + c) 包含未初始化的数据,这些数据可能不是 NULL。您将该值保存到 command 并将其传递给 reallocrealloc 只是发现所提供的指针不是 NULL,因此它假设您正在重新分配内存,但实际上不是。因此崩溃。

据我了解你的代码,你稍后将 command 分配给 *(commands + c) 所以之前的 command = *(commands + c) 只是多余的,将其更改为 command = NULL 可能会解决您的问题。

关于c - 为三维数组动态分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309736/

相关文章:

在我的代码中找不到最大值和最小值

PHP数组数组存储mysql数据

javascript - Chrome 不支持 TypedArray.prototype.slice()

c - 我在函数中分配内存,并返回 char*,我应该如何释放它?

c++ - 释放内存时堆损坏

c - 内存分配与数组大小定义

c - unsigned char 的数组声明

我可以通过更好的数组操作来优化此代码吗?

mysql - 我可以在结果为 `IN (1,2,NULL)` 的 MySQL select 语句中使用 NULL 吗?

c - 扫描指针数组问题