c - 意外的 free() 行为

标签 c malloc free

我正在自学 C。现在我正在制作一个 shell,部分基于

https://brennan.io/2015/01/16/write-a-shell-in-c/

我试图像在 bash 中一样添加管道,并创建一个名为“nospace”的函数来消除参数之间的空格,以便 strtok 将根据“|”进行分隔。

char* nospace(char *thestring)
{
char* returnline=(char*)malloc(sizeof(char)*50);
int charpos;
charpos=0;

while(*thestring != '\0')
{
    if(*thestring!=' '){
        returnline[charpos]=*thestring;
        charpos++;
    }


    thestring++;
}

returnline[charpos]='\0';


return returnline;
}

由于我使用 malloc 作为返回行,所以我需要在某处释放它,因此由于 read_args 调用 nospace,我在 read_args 中释放了它。

char** read_args(char* line)
{
argamounts=0;
//tokens and strtok was taken from tutorialspoint regarding the strtok    function
char**returnargs = (char**) malloc(sizeof(char*)*20);
char* token;

char* linenospace=nospace(line);

//printf("%s\n",linenospace);


token=strtok(linenospace,"|");
//printf("%s first token\n",token);
int argsub=0;

while (token!=NULL)
{
    returnargs[argsub]=token;
    //printf("%s\n",token); //test that all arguments are read
    argamounts++;
    token=strtok(NULL,"|");
    //printf("%s second token\n",token);
    argsub++;
}

//printf("%d",argamounts);
//returnargs[0]=line; //assumes only one arg for now

//cannot free memory here or returnargs is null, why?
//free(linenospace);

//printf("%s returnarg0\n", returnargs[0]);
//printf("%s returnarg1\n", returnargs[1]);
return returnargs;
}

但是 shell 没有读取参数,并且在插入所有 printf 以找出参数失败的位置后,我意识到释放“linenospace”会删除我的参数。因此,如果 strtok 返回一个设置为 token 的指针,并且返回参数的“元素”是 token 指针,那么“linenospace”被释放的方式是我必须在 shell 循环函数中释放双指针吗?

void ypsh_loop(void)
{
  char *line;
  char **args;
  int status;

  do {
  printf("ypsh > ");
  line = read_line();
  args=read_args(line);
  status=shexecute(args);

  }while(status);

  free(line);
  free(args);
}

(我想我必须更改 free(args); 行以释放双指针)。

<小时/>

实际上,在写这个问题的过程中,我在快速搜索SO(CentOS是我的家庭操作系统)后下载了valgrind并检查了内存泄漏。果然有一个并且正在改变“free(args);”至

int freedouble;
for(freedouble=0; freedouble<argamounts; freedouble++)
{
    free(args[freedouble]);
}

free(args);

其中argamounts是一个全局管理变量似乎已经解决了这个问题。我想这回答了我的问题,但无论如何我都会将其发布在这里。

<小时/>

编辑:

显然循环函数需要这样编写:

void ypsh_loop(void)
{
  char *line;
  char **args;
  int status;

  do {
  printf("ypsh > ");
  line = read_line();
  args=read_args(line);
  status=shexecute(args);

     free(line);
     free(args[0]);
     free(args);

  }while(status);


}

将 free() 语句移至 do while 循环中,而不是移至它们过去所在的外部,这是有意义的,因为 shell 不断循环返回,如果我继续进行 malloc,则需要一遍又一遍地释放。

但是,由于某种原因,如果我循环遍历所有参数并尝试释放它们,我会从 valgrind 得到“Invalid free()”。我必须释放 args[0] 或内存泄漏,但我只能释放 args[0] 而不能再释放更多。

添加:

    printf("amount of args %i\n",argamounts);
    int freedouble;
    for(freedouble=0; freedouble<argamounts; freedouble++)
    {
        printf("argument %d is %s ",freedouble,args[freedouble]);
        //free(args[freedouble]);
    }

进入 do while 循环检查所有参数是否都已注册表明它们都已注册,但我无法一一释放它们。一旦我弄清楚原因,我将再次编辑此内容,但如果有人知道,请告诉我。

最佳答案

However, for some reason if I loop through all the args and try to free them I get "Invalid free()" from valgrind. I have to free args[0] or the memory leaks, but I can only free args[0] and no more.

你不能释放args[1]等,因为你还没有对它们进行malloc。关于 args[0],您还没有完全对其进行 malloc,但 args[0] 指向由 分配的内存空间中的第一个标记linenospace=nospace(line),通常位于其开头(除非该行以 | 开头),因此您主要可以滥用 args[0] 来释放由 nospace(line) 分配的内存。

但是,nospace(line) 是无用的,因为删除了所有空格的命令,即。 e.所有连接的参数都无法识别(除非没有参数)。因此,我建议从您的程序中完全删除 nospace() ;那么也不用担心额外的内存分配。

关于c - 意外的 free() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41070416/

相关文章:

c - 更新函数中的字符串数组 - C

c - 如何使用 malloc 定义二维数组并将其传递给函数

c++ - malloc() 与 HeapAlloc()

free() 上损坏的双链表

c - 在 C 中释放()malloc'ed 二维数组的最佳方法

c - 传递地址,但它的工作方式就像 C 中的值调用?

C printf 数组并获取 open_stackdumpfile 错误

c - 为什么要强制转换 malloc 的返回值?

c - 为什么指针数组的 free() 不释放内存?

c - True Type 字体文件