使用 makefile 泄漏
Leaks using makefile
我是 C 编程的初学者,我遇到了 valgrind 的问题,这是我的代码:
#include <stdio.h>
int main(int argc,char **argv)
{
printf("Hello World ");
}
当我对 运行 执行 Makefile 时:
run: compile
./a.out
compile:
gcc test.c
我 运行 使用 valgrind make 并且我有一个错误告诉我还有一个仍然可以访问的块,
当我在没有 Makefile 的情况下编译时出现 0 错误,有人可以帮我解决吗?
在某些系统上,C 编程语言的标准 I/O 层会分配 valgrind 报告的缓冲区,即使对于像您这样的小程序也是如此。
我用过这样的系统。要释放标准 I/O 资源,您可以在从 main
返回之前关闭标准 I/O 流,如 fclose
的 C99 7.21.5.1 描述中所述
A successful call to the fclose function causes the stream pointed to by stream to be
flushed and the associated file to be closed. Any unwritten buffered data for the stream
are delivered to the host environment to be written to the file; any unread buffered data
are discarded. Whether or not the call succeeds, the stream is disassociated from the file
and any buffer set by the setbuf or setvbuf function is disassociated from the stream
(and deallocated if it was automatically allocated).
该段末尾的注释是提示。
int main(int argc, char **argv) {
/* ... */
fclose(stdin);
fclose(stdout);
fclose(stderr);
return 0;
}
如果 运行
我可以重现你的问题
valgrind make
但如果我 运行
make compile
valgrind ./a.out
第一个测试的是完全不同的东西(make
的内存使用情况),第二个测试(./a.out
的内存使用情况)。
和@MadScientist 一样,我真的不明白为什么人们会创建普通的 run
目标,但对我来说,他们会创建一个目标来使用 valgrind 进行测试确实有些道理。如果您想要这样的目标,那么它看起来像这样(根据 Makefile 的其余部分):
leakcheck: compile
valgrind ./a.out
我是 C 编程的初学者,我遇到了 valgrind 的问题,这是我的代码:
#include <stdio.h>
int main(int argc,char **argv)
{
printf("Hello World ");
}
当我对 运行 执行 Makefile 时:
run: compile
./a.out
compile:
gcc test.c
我 运行 使用 valgrind make 并且我有一个错误告诉我还有一个仍然可以访问的块, 当我在没有 Makefile 的情况下编译时出现 0 错误,有人可以帮我解决吗?
在某些系统上,C 编程语言的标准 I/O 层会分配 valgrind 报告的缓冲区,即使对于像您这样的小程序也是如此。
我用过这样的系统。要释放标准 I/O 资源,您可以在从 main
返回之前关闭标准 I/O 流,如 fclose
A successful call to the fclose function causes the stream pointed to by stream to be flushed and the associated file to be closed. Any unwritten buffered data for the stream are delivered to the host environment to be written to the file; any unread buffered data are discarded. Whether or not the call succeeds, the stream is disassociated from the file and any buffer set by the setbuf or setvbuf function is disassociated from the stream (and deallocated if it was automatically allocated).
该段末尾的注释是提示。
int main(int argc, char **argv) {
/* ... */
fclose(stdin);
fclose(stdout);
fclose(stderr);
return 0;
}
如果 运行
我可以重现你的问题valgrind make
但如果我 运行
make compile
valgrind ./a.out
第一个测试的是完全不同的东西(make
的内存使用情况),第二个测试(./a.out
的内存使用情况)。
和@MadScientist 一样,我真的不明白为什么人们会创建普通的 run
目标,但对我来说,他们会创建一个目标来使用 valgrind 进行测试确实有些道理。如果您想要这样的目标,那么它看起来像这样(根据 Makefile 的其余部分):
leakcheck: compile
valgrind ./a.out