在Valgrind中丢失零字节 [英] Zero bytes lost in Valgrind

查看:86
本文介绍了在Valgrind中丢失零字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Valgrind报告丢失了字节,这是什么意思?

What does it mean when Valgrind reports o bytes lost, like here:

==27752== 0 bytes in 1 blocks are definitely lost in loss record 2 of 1,532

我怀疑这只是创造性地使用malloc的人工产物,但可以肯定的是(-;

I suspect it is just an artifact from creative use of malloc, but it is good to be sure (-;

当然,真正的问题是是否可以忽略它,还是应该通过释放这些缓冲区来解决此有效泄漏.

Of course the real question is whether it can be ignored or it is an effective leak that should be fixed by freeing those buffers.

推荐答案

是的,这是一个真正的泄漏,应该修复.

Yes, this is a real leak, and it should be fixed.

当您使用malloc(0)时,malloc可能为您提供NULL,或者保证您提供的地址是与其他任何物体都不同.

When you malloc(0), malloc may either give you NULL, or an address that is guaranteed to be different from that of any other object.

由于您可能使用Linux,因此获得了第二名.分配的缓冲区本身没有浪费空间,但是libc必须做一些内务处理,这确实浪费了空间,因此您不能无限期地继续进行malloc(0).

Since you are likely on Linux, you get the second. There is no space wasted for the allocated buffer itself, but libc has to do some housekeeping, and that does waste space, so you can't go on doing malloc(0) indefinitely.

您可以通过以下方式观察它:

You can observe it with:

#include <stdio.h>
#include <stdlib.h>
int main() {
  unsigned long i;
  for (i = 0; i < (size_t)-1; ++i) {
    void *p = malloc(0);
    if (p == NULL) {
      fprintf(stderr, "Ran out of memory on %ld iteration\n", i);
      break;
    }
  }
  return 0;
}

gcc t.c && bash -c 'ulimit -v 10240 && ./a.out'
Ran out of memory on 202751 iteration

这篇关于在Valgrind中丢失零字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆