简体   繁体   English

动态分配三维数组的内存

[英]Dynamically allocating memory for three dimensional array

I don't get it. 我不明白。 I am looking since hours on following function and don't get why I get a memory fault when walking through the array in the end. 我正在寻找以下几个小时的功能,并且不知道为什么我在最后遍历数组时会出现内存故障。 If I don't walk through it, it runs without error. 如果我没有通过它,它运行没有错误。 The function should split the input string first by "|" 该函数应首先用“|”拆分输入字符串 and then by " ", putting all stuff in a 3 dimensional array. 然后用“”将所有东西放在一个三维数组中。 eg a string like "ls -l | grep test" should be splitted in every command in first dimension, every argument in second dimension. 例如,像“ls -l | grep test”这样的字符串应该在第一维中的每个命令中分开,第二维中的每个参数都应该分开。 Third for strings. 第三个字符串。

I think the error is in dynamically allocating memory. 我认为错误在于动态分配内存。 But I don't get it. 但我不明白。

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;
}

I haven't read thorough your code. 我没有仔细阅读你的代码。 But there is at least one issue I've found. 但我发现至少有一个问题。

You're using realloc to allocate and resize memory. 您正在使用realloc来分配和调整内存大小。 The realloc function is generally used to resize memory. realloc函数通常用于调整内存大小。 But if you pass a null pointer to it, it will directly allocate memory instead. 但是如果你传递一个空指针,它将直接分配内存。

The issue in your code is here: 代码中的问题在于:

    //  -> +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));

At each round of iteration you reallocated commands to a larger array, this is fine. 在每轮迭代中,您将commands重新分配给更大的数组,这很好。 But the issue is the allocated memory cell *(commands + c) contains uninitialized data, which may not be NULL. 但问题是分配的内存单元*(commands + c)包含未初始化的数据,可能不是NULL。 You saved that value to command and passed it to realloc . 您将该值保存到command并将其传递给realloc realloc just finds the pointer provided is not NULL so it assumes you are reallocating memory, which is in fact not. realloc只是发现提供的指针不是NULL,所以它假设你正在重新分配内存,实际上并非如此。 Thus a crash. 因此崩溃。

As I've understanded your code, you assigned command to *(commands + c) later so the previous command = *(commands + c) is just redundant, change it to command = NULL may fix your problem. 由于我已经完成了您的代码,您之后将command分配给*(commands + c)因此上一个command = *(commands + c)只是冗余,将其更改为command = NULL可以解决您的问题。

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

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