C ++ new/new [],它如何分配内存? [英] C++ new / new[], how is it allocating memory?

查看:92
本文介绍了C ++ new/new [],它如何分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在想知道这些指令如何分配内存.

I would like to now how those instructions are allocating memory.

例如,如果我得到代码怎么办:

For example what if I got code:

x = new int[5]; 
y = new int[5];

如果分配了这些内存,它在RAM中的实际外观如何? 是为两个变量共享整个块还是为每个变量保留块(内存页或调用方式-32bit大小为4KB)?

If those are allocated how it actually looks like in RAM? Is whole block reserved for each of the variables or block(memory page or how-you-call-it - 4KB of size on 32bit) is shared for 2 variables?

我在任何手册中都找不到我的问题的答案.感谢您的所有答复.

I couldn't find answer for my question in any manual. Thanks for all replies.

我在维基百科上发现: 页面内部碎片 很少有过程需要使用确切数量的页面.结果,最后一页可能仅会部分填充,从而浪费了一些内存.较大的页面大小显然会增加这种浪费内存的可能性,因为更多潜在的未使用内存部分已加载到主内存中.较小的页面大小可确保与分配中所需的实际内存量更加匹配. 例如,假定页面大小为1024KB.如果一个进程分配了1025KB,则必须使用两个页面,这将导致1023KB的未使用空间(其中一个页面完全消耗了1024KB,而另一个页面只有1KB).

I found on wikipedia: Internal fragmentation of pages Rarely do processes require the use of an exact number of pages. As a result, the last page will likely only be partially full, wasting some amount of memory. Larger page sizes clearly increase the potential for wasted memory this way, as more potentially unused portions of memory are loaded into main memory. Smaller page sizes ensure a closer match to the actual amount of memory required in an allocation. As an example, assume the page size is 1024KB. If a process allocates 1025KB, two pages must be used, resulting in 1023KB of unused space (where one page fully consumes 1024KB and the other only 1KB).

那是我的问题的答案.无论如何,谢谢大家.

And that was answer for my question. Anyway thanks guys.

推荐答案

典型的分配器实现将首先调用操作系统以获取巨大的内存块,然后满足您的请求,它将为您提供一部分内存,这称为子分配.如果内存不足,它将从操作系统中获取更多信息.

A typical allocator implementation will first call the operating system to get huge block of memory, and then to satisfy your request it will give you a piece of that memory, this is known as suballocation. If it runs out of memory, it will get more from the operating system.

分配器必须跟踪从操作系统获得的所有大块,以及分发给客户端的所有小块.它还必须接受来自客户端的阻止.

The allocator must keep track of both all the big blocks it got from the operating system and also all the small blocks it handed out to its clients. It also must accept blocks back from clients.

典型的子分配算法会保留一个称为 freelist 的每个大小的返回块的列表,并且始终尝试满足来自freelist的请求,仅在freelist为空时才转到主块.这种特殊的实现技术对普通程序来说非常快且非常有效,尽管如果请求大小到处都是(这种情况对于大多数程序来说并不常见),但它具有令人讨厌的碎片属性.

A typical suballocation algorithm keeps a list of returned blocks of each size called a freelist and always tries to satisfy a request from the freelist, only going to the main block if the freelist is empty. This particular implementation technique is extremely fast and quite efficient for average programs, though it has woeful fragmentation properties if request sizes are all over the place (which is not usual for most programs).

像GNU的malloc实现那样的现代分配器很复杂,但是已经积累了数十年的经验,应该被认为是非常好的,以至于很少需要编写自己的专用子分配器.

Modern allocators like GNU's malloc implementation are complex, but have been built with many decades of experience and should be considered so good that it is very rare to need to write your own specialised suballocator.

这篇关于C ++ new/new [],它如何分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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