繁体   English   中英

动态分配三维数组的内存

[英]Dynamically allocating memory for three dimensional array

我不明白。 我正在寻找以下几个小时的功能,并且不知道为什么我在最后遍历数组时会出现内存故障。 如果我没有通过它,它运行没有错误。 该函数应首先用“|”拆分输入字符串 然后用“”将所有东西放在一个三维数组中。 例如,像“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并将其传递给realloc realloc只是发现提供的指针不是NULL,所以它假设你正在重新分配内存,实际上并非如此。 因此崩溃。

由于我已经完成了您的代码,您之后将command分配给*(commands + c)因此上一个command = *(commands + c)只是冗余,将其更改为command = NULL可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM