首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >函数中指向char数组的Realloc指针

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

Stack Overflow用户
提问于 2014-12-06 09:37:18
回答 1查看 2.9K关注 0票数 2

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

下面是调用该函数的代码的一部分:

代码语言:javascript
运行
复制
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);
    }
}

其职能如下:

代码语言:javascript
运行
复制
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;
}

使用以下命令的val差事报告:val差令

代码语言:javascript
运行
复制
==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));都失败了,但我不知道原因。

提前感谢您的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-06 11:00:26

市长问题:

代码语言:javascript
运行
复制
*commands[i]=malloc(100);

应:

代码语言:javascript
运行
复制
(*commands)[i]=malloc(100);

因为[]*更紧密地结合。

同样的问题是:

代码语言:javascript
运行
复制
strcpy(*commands[i],*line);

在这里:

代码语言:javascript
运行
复制
printf("COMMAND: %s\n",*commands[i]);

如上面所示,修复这两个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27330275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档