How much memory does malloc(0) allocate?
来源:互联网 发布:下载itunes软件 编辑:程序博客网 时间:2024/05/01 12:04
原文:http://prog21.dadgum.com/179.html
On most systems, this little C program will soak up all available memory:
while (1) { malloc(0);}
malloc(0)
, let's look at the simpler case ofmalloc(1)
.There's an interesting new C programmer question about
malloc
: "Given a pointer to dynamically allocated memory, how can I determine how many bytes it points to?" The answer, rather frustratingly, is "you can't." But when you callfree
on that same pointer, the memory allocator knows how big the block is, so it's storedsomewhere. That somewhere is commonly adjacent to the allocated memory, along with any other implementation-specific data needed for the allocator.In the popular
dlmalloc
implementation, between 4 and 16 bytes of this overhead are added to a request, depending on how the library is configured and whether pointers are 32 or 64 bits. 8 bytes is a reasonable guess for a 64-bit system.To complicate matters, there's a minimum block size that can be returned by
malloc
. Alignment is one reason. If there's an integer size secretly prepended to each block, then it doesn't make sense to allocate a block smaller than an integer. But there's another reason: when a block is freed, it gets tracked somehow. Maybe it goes into a linked list, maybe a tree, maybe something fancier. Regardless, the pointers or other data to make that work have to go somewhere, and inside the just-freed block is a natural choice.In
dlmalloc
, the smallest allowed allocation is 32 bytes on a 64-bit system. Going back to themalloc(1)
question, 8 bytes of overhead are added to our need for a single byte, and the total is smaller than the minimum of 32, so that's our answer:malloc(1)
allocates 32 bytes.Now we can approach the case of allocating zero bytes. It turns out there's a silly debate about the right thing to do, and it hasn't been resolved, so technically allocating zero bytes is implementation-specific behavior. One side thinks that
malloc(0)
should return a null pointer and be done with it. It works, if you don't mind a null return value serving double duty. It can either mean "out of memory" or "you didn't request any memory."The more common scheme is that
malloc(0)
returns a unique pointer. You shouldn't dereference that pointer because it's conceptually pointing to zero bytes, but we know from our adventures above that at leastdlmalloc
is always going to allocate a 32 byte block on a 64-bit system, so that's the final answer: it takes 32 bytes to fulfill your request for no memory.[EDIT: I modified the last two paragraphs to correct errors pointed out in email and a discussion thread onreddit. Thank you for all the feedback!]
(If you liked this, you might enjoy Another Programming Idiom You've Never Heard Of.)
0 0
- How much memory does malloc(0) allocate?
- JavaCard free memory detect (how much memory left)
- Joel test : how much does your company get?
- How much carbon dioxide does a Google search query produce?
- How Much Bandwidth Does Your Website Really Need
- calloc, malloc, free, realloc - Allocate and free dynamic memory
- how to prevent ZFS from eating too much memory
- How much GPU memory do iPhones and iPads have?
- iOS memory allocation - how much memory can be used in an application?
- How much math does a GIS Analyst need to know?(转)
- sun jdk7 有问题 Native memory allocation (malloc) failed to allocate
- if i don't close handle after a process created, how much memory will leak?
- checkio (How much gold)
- Allocate aligned memory
- Memory allocate in Ogre
- MPI_Probe-Dynamically allocate memory
- Android cannot allocate memory
- Gerrit 执行 git pull 的时候,提示 fatal: Out of memory, malloc failed (tried to allocate *** bytes)错误的解决方法
- ViewPage :Required method instantiateItem was not overridde
- 表单--form标签
- QT多线程
- Farpoint_Spread5_WinForm中文帮助文档
- split STL string by chars
- How much memory does malloc(0) allocate?
- PHP制作验证码
- 利用FTP和WEBSERVICE实现数据库和用户文件的备份
- vsftp 用户登录ftp登录指定目录
- 细梳腾讯帝国幼年艰辛,竟几度面临夭折
- 配置struts2过滤器 session失效跳转到登陆页面
- 理解HTTP session原理及应用
- 看java核心技术总结:怎样设计类使它具有oop的专业水准
- 友盟自定义分享-生成带图片的二维码,自定义分享布局