Is there a way to get the range of memory addresses available on the heap?

It seems to me that this is how memory works in C ++:

If you use new , you will ask the compiler implementation to provide you some memory (any memory) from the heap.

If you use the new allocation syntax, you are asking to redistribute a specific memory cell that you already know, an address (suppose it is also from a heap), which was supposedly also originally allocated from new at some point.

My question is:

Do you need to know which memory cells are available for your a priori program (i.e., without reallocating memory from the heap that was already provided to you by the new operator)?

Is heap memory continuous? If so, can you find out where it starts and where it ends?

ps Just try to get as close to metal as possible ...

+4
source share
4 answers

Not in any portable mode. Modern operating systems typically use paging (or virtual memory), so the amount of available memory is not a question that can be easily answered.

There is no need for memory on the heap to be contiguous if you need you to write your own heap, which is not so difficult to do.

+4
source

The memory available to your program a priori contains the variables that you defined. The compiler calculated exactly how much the program needed. There is nothing superfluous that you can use for something else.

New objects that need to be created dynamically are allocated from the free repository (aka heap), possibly using new , but most often using containers from the library, for example std::vector .

The language standard does not say anything about how this works in detail, how it can be used.

+2
source

That's a very difficult question. In a modern operating system, there is such a subsystem as a memory manager. When your program executes the new operator, there are two options:

  • If there is enough memory for the program, you get a pointer to the memory in your bunch of programs
  • If there is not enough memory, the execution is set to the memory manager of the operating system, and it decides what to do: give more memory to your program (let it be said that it will change the size of your heap) or refuse and throw an exception.

In any case, to know which memory cells are available for your a priori program (i.e., without reallocating the memory from the heap that was already provided to you by the new operator)?

I want to emphasize that this depends on the OS version and environment.

Is heap memory continuous?

No, it may not be contiguous.

+1
source

The priority of addresses received from consecutive calls to new or malloc() is not defined. The runtime and the C operating system can easily return pointers over the entire address space from consecutive new s. (And actually, this is likely to happen, since good allocators are retrieved from different pools depending on the size of the distribution to reduce fragmentation, and these pools will be on different pages.)

However, bytes within the same allocation in new guarantee that they will be contiguous, so if you do

 int *foo = new int[1024 * 1024] 

you will get a million related words.

If you really need a lot of contiguous selection, you probably have to use operating system-specific functions to do this (unless someone has hidden it behind some Boost library that I don't know about). On Windows VirtualAlloc () . In POSIX mmap () .

+1
source

All Articles