开发者

C++ new / new[], how is it allocating memory?

开发者 https://www.devze.com 2023-01-29 22:48 出处:网络
I would like to now how those instructions are allocating me开发者_运维技巧mory. For example what if I got code:

I would like to now how those instructions are allocating me开发者_运维技巧mory.

For example what if I got code:

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

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.

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.

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).

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.


You didn't find it in the manual because it's not specified by the standard. That is, most of the time x and y will be side by side (go ahead and cout<< hex << their addresses).

But nothing in the standard forces this so you can't rely on it.


Each process has different segments associated which are divided among the process address space : 1) The text segment :: Where your code is placed 2) Stack Segment :: Process stack 3) Data Segment :: This is where memory by "new" is reserved. Besides that it also store initialized and uninitialized static data (bss etc).

So , whenever you call a new function (which i guess uses malloc internally , but the new class makes it much safer to handle memory) it allocates the specified number of bytes in the data segment. Ofcourse the address you print while running the program is virtual and needs to be translated to physical address..but that is not our headache and the OS Memory management unit does that for us.

0

精彩评论

暂无评论...
验证码 换一张
取 消