gpt4 book ai didi

c - C : valgrind report shows additional allocs that can't be freed 中的线程

转载 作者:太空宇宙 更新时间:2023-11-04 04:41:10 25 4
gpt4 key购买 nike

我用 C 编写了一个程序。在一个 for 循环中它创建了 12 个线程。

   for (i = 0; i < 12; i++)
{
status=pthread_create(&ntid[i],NULL,th_f,NULL);
if (status != 0)
{
printf("Error in Creating Thread\n");
exit(1);
}
}

然后我加入他们,如图所示:

   for (i = 0; i < 12; i++)
{
ret=pthread_join(ntid[i],&retval);
if (ret)
{
printf("Error joining\n");
exit(1);
}
printf("Thread terminated with status %d\n", *((int*)retval));
free (retval);
}

线程正在执行的函数是 th_f 。它有 1 个 malloc,并且在加入后被释放(如您在 `free (retval) 中看到的那样);

该程序的 valgrind 报告如下:

==27123== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==27123== malloc/free: in use at exit: 1,552 bytes in 5 blocks.
==27123== malloc/free: 29 allocs, 24 frees, 4,864 bytes allocated.
==27123== For counts of detected errors, rerun with: -v
==27123== searching for pointers to 5 not-freed blocks.
==27123== checked 89,896 bytes.

==27123== LEAK SUMMARY:
==27123== definitely lost: 0 bytes in 0 blocks.
==27123== possibly lost: 0 bytes in 0 blocks.
==27123== still reachable: 1,552 bytes in 5 blocks.
==27123== suppressed: 0 bytes in 0 blocks.

所以我需要这些问题的答案:

  1. 应该只有 12 次分配和 12 次释放。为什么这个数字加倍了?
  2. 无论创建多少个线程,5 个分配总是额外的。他们没有被释放。为什么?

最佳答案

额外的分配和释放可能是线程堆栈。额外的五个可能由线程库在内部使用——正如它所说,它们没有泄漏但仍然可以访问。

关于c - C : valgrind report shows additional allocs that can't be freed 中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com