简体   繁体   English

代码片段以查找程序的堆段或堆栈段的大小

[英]Code snippet to find the size of heap segment or stack segment for a program

I was asked this question in a interview, what is the minimum size malloc can allocate and what is the max size a malloc can allocate. 采访中有人问我这个问题,malloc可以分配的最小大小是多少,malloc可以分配的最大大小是多少。

I answered min size if 1 byte , and when I told that he replied assuming that is correct now tell me what is the max size memory chunk can malloc allocate( when you try doing malloc it fails sometime), think you have 4GB of RAM with you write me a code to find the size of heap segment for your simple program you write or write me a code to find the size of stack segment. 我回答了最小大小(如果为1 byte ,当我告诉他他假设正确时回答时,请告诉我malloc可以分配的最大大小的内存块(当您尝试执行malloc有时会失败),以为您有4GB的RAM您给我写了一个代码,以查找您的简单程序的堆段的大小,或者给我写了一个代码,以查找堆栈段的大小。

Can any one help me in this solution. 任何人都可以帮我解决这个问题。

Is the minimum size it can allocate is 1 byte ? 它可以分配的最小大小是1 byte吗? I answered thinking char requires the least memory. 我回答说char需要最少的内存。

Yes, the minimum size malloc(size_t) is guaranteed to be able to allocate it (size_t) 1 . 是的,保证最小大小malloc(size_t)能够分配它(size_t) 1

It is up to the implementation whether requesting 0 bytes would return NULL or not. 请求0字节是否返回NULL取决于实现。

C standard (WG14/N1256) 7.20.3: C标准(WG14 / N1256)7.20.3:

If the size of the space requested is zero, the behavior is implementationdefined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object 如果所请求空间的大小为零,则行为是由实现定义的:返回空指针,或者行为类似于该大小为某个非零值,不同之处在于不得使用返回的指针来访问对象

The minimum size malloc can really allocate is 1 byte. malloc可以实际分配的最小大小为1个字节。 According to the C standard (7.22.3), "If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object." 根据C标准(7.22.3),“如果请求空间的大小为零,则行为是实现定义的:返回空指针,或者行为类似于该大小为非零值,除了返回的指针不得用于访问对象。”

On the other hand, malloc almost definitely will allocate more bytes than 1, even if you call it with size 1. It needs extra bytes for storing additional information, eg the size of the allocated memory chunk, which will be needed for deallocation. 另一方面,即使您使用大小1调用malloc几乎也肯定会分配比1多的字节。它需要额外的字节来存储其他信息,例如,分配所需的存储块的大小。 Furthermore, there are memory alignment issues. 此外,还有内存对齐问题。

If you were asked to write a program to compute the maximum size malloc can allocate on a given machine of 4GB memory and at a given time, I think you were expected to write a program that used binary search to find the exact maximum. 如果要求您编写一个程序来计算malloc可以在给定时间的4GB内存的给定机器上分配的最大大小,那么我认为您应该编写一个使用二进制搜索来找到确切最大值的程序。 Of course, the answer of the program could be different each time you called it. 当然,每次调用该程序时,答案都可能不同。 The same could be done using a stack-allocated variable-sized array, to compute the maximum allowed size for your stack. 使用堆栈分配的可变大小的数组来计算堆栈允许的最大大小,可以执行相同的操作。

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

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