我有一个程序,它从一个文件中读取并将项目存储在队列中,然后将它们打印出来。我从 valgrind 得到这个:
HEAP SUMMARY:
in use at exit: 302 bytes in 14 blocks
total heap usage: 30 allocs, 16 frees, 1,230 bytes allocated
302 bytes in 14 blocks are definitely lost in loss record 1 of 1
at 0x4A05FDE: malloc (vg_replace_malloc.c:236)
by 0x372167FB41: strdup (strdup.c:43)
by 0x400A6A: addtoqueue(main.c:30)
by 0x400B5A: addfiletoqueue(main.c:45)
by 0x400C27: main (main.c:62)
LEAK SUMMARY:
definitely lost: 302 bytes in 14 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 0 bytes in 0 blocks
still reachable: 0 bytes in 0 blocks
suppressed: 0 bytes in 0 blocks
main.c的第62行
addfiletoqueue (queue, argv[argi]);
main.c的第45行
addtoqueue (queue, file, filename);
main.c 的第 30 行
readline = strdup (buffer);
assert (readline != NULL);
queue_add (queue, readline);
我需要免费阅读线吗?
最佳答案
是的,strdup()
动态分配您必须 free()
的内存。从链接的引用页面:
The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).
关于c - 我不是在某处释放内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063871/