【发布时间】:2019-05-05 20:52:05
【问题描述】:
当我运行 valgrind 时,我收到以下消息:
大小为 4 的无效读取
在getdelim
通过 getline
按主线(第 y 行)
地址未被堆栈、malloc 或(最近)释放
int main() {
char *line = NULL;
size_t len = 0;
ssize_t read = 0;
FILE *fp = fopen(argv[2], "r");
read = getline(&line, &len, fp); //this is the line that gives error on valgrind
free(line);
fclose(fp);
}
为什么会这样?
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后学习如何创建minimal reproducible example 向我们展示。
-
更具体地说,请将 full Valgrind 输出复制粘贴到问题正文中,无需修改。作为文本。然后包括一个展示问题的实际程序,而不是一些伪代码。您当前显示的代码缺少关键信息和上下文。
-
这可能是个愚蠢的问题,但你真的向程序传递了三个命令行参数吗?
-
这怎么编译?
-
无法编译:
error: ‘argv’ undeclared (first use in this function)