Realloc指向函数中char数组的指针

时间:2014-12-06 09:37:19

标签: c arrays pointers char realloc

我在函数中遇到realloc问题,我将指针传递给char数组(或者至少,我认为是这样,我可能错过了做它)。如果我在我的代码中编写它的方式是错误的,我打算做的是将char数组传递给我的函数,然后函数应该填充数组并扩展它的大小,然后我应该能够使用填充的值在我主要的阵列中。

这是调用函数的代码部分:

int main(int argc, char *argv[])
{
    int taille_commands;
    int taille_tableau=1;
/*  if (signal (SIGINT, sig_handler) == SIG_IGN){

    }
*/  char *line=malloc(BUFSIZ);
    char *root=malloc(BUFSIZ);
    char *result=malloc(BUFSIZ);
    char **commands = malloc(1 * sizeof *commands);
    taille_commands=init(&argc,argv,&root,&line,&result,&commands);
    if (taille_commands<=0){
        exit(1);
    }
}

函数init:

int init(int *argc, char *argv[],char **root, char **line,char **result, char **commands[]){
//check that the number of arguments given is valid
    if (*argc != 2){
        printf("Error: arguments. \nThe file should only take one argument which is the name of the level\n");
        return-1;
    }
    char test[5]="T\0";
    int nb_lettres=strlen(argv[1]);
    strncpy(test,argv[1]+nb_lettres-4,4);
    //check that the file given is either a .tgz or a .tar
    test[4]='\0';
    if(strcmp(test,".tar")!=0 && strcmp(test,".tgz")!=0)
    {
        printf("Error: arguments. \nThe argument should be a file having the extension .tar or .tgz \n");
        return-2;
    }
    int status;
    pid_t pid;
    //create the folder then move to it, then extract the tar
    pid=fork();
    if(pid==0){
        if(fork()==0){
            execlp("mkdir","mkdir","leaSHdir",NULL);
        }
        //waiting to make sure we don't try to go in the folder before it's fully created
        wait(&status);
        execlp("tar","tar", "-xf", argv[1], "-C", "leaSHdir/",NULL);
    }
    waitpid(pid,&status,0);
    printf("Extracting the files..\n");
    //Read the meta file
    FILE *file;
    chdir("./leaSHdir");
    *root=getcwd(0,0);
    if(*root==NULL){
        printf("An error occured while getting root");
        exit(-5);
    }
    file=fopen("meta","r");
    if (file==NULL){
        printf("Error: meta. \nImpossible to read the meta file. Please check that it does exist (without looking in, vile cheater)\n");
        return-3;

    }
    size_t len=0; 
        //Saving the commands which will be used by the user

    int i=0;
    if(*commands==NULL){
        printf("Error: memory. \nA problem occured with the memory while creating a pointer\n");
        return-4;
    }
    int taille_commands=1;
    char *lineTempo=NULL;
    while(getline(&lineTempo,&len,file)!=-1){
        if(strstr(lineTempo,"$")!=NULL){
            strcpy(*line,lineTempo+2);
            *line=strtok(*line,"\n");
            *commands[i]=malloc(100);
            strcpy(*commands[i],*line);
            printf("COMMAND: %s\n",*commands[i]);
                    //if the array is full,we add space for the next incoming command
            if(i >= 1){
                *commands=realloc(*commands,sizeof *commands *(i+2));
                if(*commands==NULL){
                    printf("Do something about it being a failure\n" );
                }
                taille_commands=i+1;
            }
            i++;
        }
        else if(lineTempo[0]=='>'){
            strcpy(*result,lineTempo+2);
        }
    }
    fclose(file);
    return taille_commands;
}

使用此命令的valgrind报告: valgrind --leak-check = full --track-originins = yes --show-leak-kinds = all ./leaSH leash-simple.tgz

==26055== Memcheck, a memory error detector
==26055== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==26055== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==26055== Command: ./leaSH leash-simple.tgz
==26055== 
Extracting the files..
COMMAND: cat
==26055== Use of uninitialised value of size 8
==26055==    at 0x401C07: init (in /home/jilako/Bureau/projet_RS/leaSH)
==26055==    by 0x401E38: main (in /home/jilako/Bureau/projet_RS/leaSH)
==26055==  Uninitialised value was created by a stack allocation
==26055==    at 0x401D90: main (in /home/jilako/Bureau/projet_RS/leaSH)
==26055== 
==26055== Invalid write of size 8
==26055==    at 0x401C07: init (in /home/jilako/Bureau/projet_RS/leaSH)
==26055==    by 0x401E38: main (in /home/jilako/Bureau/projet_RS/leaSH)
==26055==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==26055== 
==26055== 
==26055== Process terminating with default action of signal 11 (SIGSEGV)
==26055==  Access not within mapped region at address 0x0
==26055==    at 0x401C07: init (in /home/jilako/Bureau/projet_RS/leaSH)
==26055==    by 0x401E38: main (in /home/jilako/Bureau/projet_RS/leaSH)

所以,我发现*commands[i]=malloc(100);*commands=realloc(*commands,sizeof *commands *(i+2));失败了,但我不明白为什么。

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:2)

市长问题:

*commands[i]=malloc(100);

应为:

(*commands)[i]=malloc(100);

,因为[]*更紧密。

同样的问题她:

strcpy(*commands[i],*line);

在这里:

printf("COMMAND: %s\n",*commands[i]);

修复这两者,如上所示。