C ++ new / new [] how to allocate memory?

I would now like to see how these instructions allocate memory.

For example, what if I received the code:

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

If they stand out the way it looks in RAM? Is a whole block reserved for each of the variables or blocks (a memory page, or as you call it - 4 KB in size by 32 bits) is divided into 2 variables?

I could not find the answer to my question in any manual. Thanks for all the answers.

I found on Wikipedia: Internal page fragmentation It is rarely required that processes require the use of the exact number of pages. As a result, the last page is likely to be only partially filled, losing some memory. Larger page sizes clearly increase potential memory loss in this way, as more potentially unused portions of memory are loaded into main memory. Smaller page sizes provide a closer match to the actual amount of memory required for distribution. As an example, suppose a page size of 1024 KB. If a process allocates 1025 KB, two pages must be used, resulting in 1023 KB of unused space (where one page consumes 1024 KB completely and the other only 1 KB).

And that was the answer to my question. Thanks anyway guys.

0
source share
3 answers

A typical allocator implementation will first call the operating system to receive a huge block of memory, and then, to satisfy your request, it will give you some of this memory, this is called suballocation . If he runs out of memory, she will get more from the operating system.

The distributor must keep track of all the large blocks that he received from the operating system, and all the small blocks that he distributed to his customers. It should also accept blocks from clients.

, freelist, , , freelist . , , ( ).

, GNU malloc, , , .

+4

, . x y ( cout<< hex << ).

, .

+4

, : 1) :: 2) :: 3) :: "". , (bss ..).

Thus, whenever you call a new function (which, I believe, uses malloc internally, but the new class makes it more secure to handle memory), it allocates the specified number of bytes in the data segment. Of course, the address that you type when you start the program is virtual and must be translated to a physical address .. but this is not our headache, and the OS memory management unit does this for us.

+1
source

Source: https://habr.com/ru/post/1779865/


All Articles