使用Malloc时额外的字节在哪里? [英] where do the extra bytes go while using Malloc ?

查看:105
本文介绍了使用Malloc时额外的字节在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在堆的情况下,为了跟踪单个内存块,它需要8个字节的信息。

也就是说,它需要4个字节来保存大小,4个字节用于保存指向下一个内存块的

指针。因此,对于每个额外的块,即使它只有一个字节长,也需要
,再次需要这8个字节,

除了存储实际需要的1个字节外chunk本身。


因此,应该浪费内存来管理链表

(堆)。

但是,然后malloc / calloc如何分配确切的数据大小

必须有一些内存的浪费,必须考虑在堆内存中分配时
(内存)用于管理

信息的链表)?


也就是说,Malloc应该消耗更多空间来分配所需的

数据量。额外的字节在哪里去?


优先购买者,

Karthik Balaguru

Hi,
In the case of heap , to keep track of a single chunk of memory it
requires 8 bytes of information.
That is, it requires 4 bytes to hold the size, and 4 bytes to hold the
pointer to the next block of memory. So, For every additional chunk,
even if it is only one byte long, these 8 bytes are required again, in
addition to the 1 byte actually needed to store the chunk itself.

So, there should be wastage of memory for managing the linked list
(heap).
But, How does malloc/calloc then allocate the exact size of data as
there must be some wastage of memory that has to be taken into account
while allocating in heap memory (Memory consumed for managing the
information in terms of linked list) ?

That is, Malloc should consume more space to allocate the desired
amount of data . where do the extra bytes go ?

Thx in advans,
Karthik Balaguru

推荐答案

karthikbalaguru写道:
karthikbalaguru wrote:



在堆的情况下,要跟踪单个块内存它

需要8个字节的信息。

也就是说,它需要4个字节来保存大小,4个字节来保存

指向下一块内存的指针。因此,对于每个额外的块,即使它只有一个字节长,也需要
,再次需要这8个字节,

除了存储实际需要的1个字节外chunk本身。
Hi,
In the case of heap , to keep track of a single chunk of memory it
requires 8 bytes of information.
That is, it requires 4 bytes to hold the size, and 4 bytes to hold the
pointer to the next block of memory. So, For every additional chunk,
even if it is only one byte long, these 8 bytes are required again, in
addition to the 1 byte actually needed to store the chunk itself.



这可能是一个实现,malloc的工作不包括

标准。不要忘记malloc返回的值是正确的

对齐任何类型。

That might be one implementation, the working of malloc are not covered
by the standard. Don''t forget the value returned by malloc is correctly
aligned for any type.


因此,应该浪费内存管理链表

(堆)。

但是,malloc / calloc如何分配确切的数据大小

必须有在堆内存中分配时需要考虑一些内存浪费

(根据链表管理

信息所消耗的内存)?
So, there should be wastage of memory for managing the linked list
(heap).
But, How does malloc/calloc then allocate the exact size of data as
there must be some wastage of memory that has to be taken into account
while allocating in heap memory (Memory consumed for managing the
information in terms of linked list) ?



你无法得到任何东西(功能)。

You can''t get something (functionality) for nothing.


也就是说,Malloc应该消耗更多的空间来分配所需的

数据量。多余字节在哪里?
That is, Malloc should consume more space to allocate the desired
amount of data . where do the extra bytes go ?



这取决于实施。


-

Ian Collins。

That''s up to the implementation.

--
Ian Collins.


karthikbalaguru写道:
karthikbalaguru wrote:

malloc / calloc如何分配确切的数据的大小为

在堆内存中分配时必须要考虑一些内存浪费

(管理
$所需的内存) b $ b关于链表的信息)?
How does malloc/calloc then allocate the exact size of data as
there must be some wastage of memory that has to be taken into account
while allocating in heap memory (Memory consumed for managing the
information in terms of linked list) ?



虽然原则上这是一个实施问题但据我所知

相当常见的是为每个管理数据提供(部分) malloc

挡在街区前面。或者至少在10-20年前我使用的系统是什么。


例如,如果你看一下malloc的代码在其中一个最新的

glibc版本中,它说:


每个分配的块的最小开销:4或8个字节。每个malloced块

有一个隐藏的开销持有大小和状态信息。


代码[1]中更详细的描述似乎表明

管理字节放在

应用程序数据块的前面和末尾。


[1]
http://www.google.com/codesearch?q=f...+details%3A%22


问候,

-

Filip Larsen


While this in principle is an implementation issue it is to my knowledge
fairly common to place (some of) this management data for each malloc
block just in front of the block. Or it was, at least, on the systems I
was using 10-20 years ago.

For example, if you look at the code for malloc in one of the latest
glibc releases it says:

"Minimum overhead per allocated chunk: 4 or 8 bytes. Each malloced chunk
has a hidden word of overhead holding size and status information."

The more detailed description in the code [1] seem to indicate that the
management bytes are placed both in front and at the end of the
application data block.

[1]
http://www.google.com/codesearch?q=f...+details%3A%22

Regards,
--
Filip Larsen


karthikbalaguru写道:
karthikbalaguru wrote:



在堆的情况下,为了跟踪单个内存块,它需要8个字节的信息。
Hi,
In the case of heap , to keep track of a single chunk of memory it
requires 8 bytes of information.



没有必要堆。开销不必像你那样b $ b状态。

There doesn''t have to be a heap. The overheads don''t have to be as you
state.


也就是说,它需要4个字节来保持大小,并且4个字节用于保存指向下一个内存块的

指针。
That is, it requires 4 bytes to hold the size, and 4 bytes to hold the
pointer to the next block of memory.



为什么? 1)如果我有这个大小,我可以(也许)找到下一块

内存,2)大小(和指针)可能不一定是4个字节长。

Why? 1) If I have the size, I can (perhaps) find the next block of
memory, 2) sizes (and pointers) may not necessarily be 4 bytes long.


所以,对于每一个额外的块,即使只有一个字节长,也需要
,再次需要这8个字节,在

中另外实际需要存储块本身的1个字节。
So, For every additional chunk,
even if it is only one byte long, these 8 bytes are required again, in
addition to the 1 byte actually needed to store the chunk itself.



正如Richard H所说,更普遍地说,管理malloc()ed内存时必须有一些

开销。 br />

开销的性质和大小取决于实现。

As Richard H has said, putting this more generally, there must be some
overhead involved in managing malloc()ed memory.

The nature and size of the overhead is dependent on the implementation.


因此,应该浪费内存来管理链表

(堆)。
So, there should be wastage of memory for managing the linked list
(heap).



它不一定是一个链表 - 其他数据结构是

可用并且可能适用。

It doesn''t have to be a linked list - other data structures are
available and may be applicable.


但是,malloc / calloc如何分配确切的数据大小

必须考虑到一些必须考虑的内存浪费

在堆内存中分配(根据链表管理

信息所消耗的内存)?
But, How does malloc/calloc then allocate the exact size of data as
there must be some wastage of memory that has to be taken into account
while allocating in heap memory (Memory consumed for managing the
information in terms of linked list) ?



它没有分配所请求数据的确切大小。它分配至少
至少。为了满足这个要求,考虑到它的管理费用b $ b b和它必须为任何数据类型提供内存对齐的合同



It doesn''t allocate the exact size of data requested. It allocates "at
least enough" to satisfy the request, taking into account its management
overheads and the contract it has to provide memory aligned
appropriately for any data type.


也就是说,Malloc应该消耗更多空间来分配所需的

数据量。多余的字节在哪里?
That is, Malloc should consume more space to allocate the desired
amount of data . where do the extra bytes go ?



实施者把它们放在哪里。对于这个问题,没有保证的一般答案

- 你需要检查特定的malloc实现。


如果你想讨论具体的实现,我建议这样做

除了comp.lang.c以外的其他地方

Where the implementers put them. There is no guaranteed general answer
to the question - you''d need to examine a specific malloc implementation.

If you wish to discuss a specific implementation, I suggest doing it
somewhere other than "comp.lang.c"


thx in advans,
Thx in advans,



如果您的意思是提前谢谢为什么不写呢?

If you mean "thanks in advance" why not write that?


这篇关于使用Malloc时额外的字节在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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