简体   繁体   English

valgrind:为什么我的小编程分配了这么多空间?

[英]valgrind: Why is my tiny programming allocating so much space?

I'm reading through chapter 4 of "Learn C the Hard Way", where we start to work with valgrind. 我正在阅读“学习艰难的道路”第4章,我们开始与valgrind合作。

One thing I noticed is that my very small programs are allocating 1,024 bytes: 我注意到的一件事是我的小程序正在分配1,024个字节:

==19896== HEAP SUMMARY:
==19896==     in use at exit: 0 bytes in 0 blocks
==19896==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated

In the book, and in other people's code it shows 0 bytes allocated. 在本书和其他人的代码中,它显示了0个字节的分配。

Here is the code: 这是代码:

#include <stdio.h>

int main(int argc, char *argv[])
{
  int distance = 100;

  // this is also a comment
  printf("You are %d miles away.\n", distance);

  return 0;
}

I don't understand why there needs to be 1kb of space allocated for this thing. 我不明白为什么需要为这件事分配1kb的空间。

This bothers me and I would like to know what is going on. 这困扰我,我想知道发生了什么。

Any help is appreciated. 任何帮助表示赞赏。 Thanks for your time! 谢谢你的时间!

Edit: 1KB, not 1MB 编辑:1KB,而不是1MB

That's 1KB, not 1MB.. which isn't much memory these days (35 years ago, it was a lot). 这是1KB,而不是1MB ..这些天没有太多记忆(35年前,它是很多)。

As to why it's using that much: printf uses buffered I/O, which allocates buffers. 至于为什么它使用那么多: printf使用缓冲的I / O,它分配缓冲区。 The exact answer really depends upon the platform since c libraries and operating systems will vary. 确切的答案实际上取决于平台,因为c库和操作系统会有所不同。 But if you were to write the same program using just system calls eg. 但是,如果你只使用系统调用编写相同的程序,例如。 write instead of printf , you'd probably see the memory usage go down. write而不是printf ,你可能会看到内存使用率下降。

printf buffers I/O before it actually writes that data to standard out (as @little_birdie mentioned). printf在将数据实际写入标准输出之前缓冲I / O(如提到的@little_birdie)。 Buffered I/O refers to the practice of temporarily storing I/O operation in your application (user-space) prior to transmitting it to the kernel, which can be slow. 缓冲I / O是指在将应用程序(用户空间)传输到内核之前临时存储I / O操作的做法,这可能很慢。 In order to minimize these so called system calls , your application will ask for such memory ahead of time. 为了最大限度地减少这些所谓的系统调用 ,您的应用程序将提前询问此类内存。

It is not uncommon for certain features of the system to disable this feature entirely, or even perhaps for a historical system to not have buffered I/O at all (although I'm not familiar with any). 系统的某些功能完全禁用此功能,甚至可能是历史系统根本没有缓冲I / O(尽管我不熟悉任何功能),这种情况并不少见。

If you want to disabled buffering on your stdout here (and thus allocation "0" bytes of heap memory), you can ask for it with setbuf like so: 如果你想在你的stdout上禁用缓冲(并因此分配堆内存的“0”字节),你可以像setbuf一样请求它:

#include <stdio.h>

int main()
{
    int distance = 100;
    setbuf(stdout, NULL);
    printf("You are %d miles away.\n", distance);
    return 0;
}

If you want to learn more about this, check out the excellent Linux Programming Interface 如果您想了解更多相关信息,请查看优秀的Linux编程接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM