Is the memory allocated by new operated consecutiv

2020-04-09 06:10发布

as the title says, I want to know in c++, whether the memory allocated by one new operation is consecutive...

标签: c++
7条回答
做个烂人
2楼-- · 2020-04-09 06:38

The memory allocated in your process's address space will be contiguous.

How those bytes are mapped into physical memory is implementation-specific; if you allocate a very large block of memory, it is likely to be mapped to different parts of physical memory.

Edit: Since someone disagrees that the bytes are guaranteed to be contiguous, the standard says (3.7.3.1):

The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size.

查看更多
不美不萌又怎样
3楼-- · 2020-04-09 06:41

The virtual addresses of the allocated bytes will be contiguous. They will also be physically contiguous within resident pages backing the address space of your process. The mapping of physical pages to regions of the process virtual space is very OS and platform specific, but in general you cannot assume physically contiguous range larger then or not aligned on a page.

查看更多
▲ chillily
4楼-- · 2020-04-09 06:42

Yes.

Don't bother about the "virtual memory" issue: apart that there could be cases when you haven't at all a system that supports virtual memory, from your PoV you get a consecutive memory chunk. That's all.

查看更多
Bombasti
5楼-- · 2020-04-09 06:43
BYTE* data = new BYTE[size];

In this code, whatever size is given, the returned memory region is consecutive. If the heap manager can't allocate consecutive memory of size, it's fail. an exception (or NULL in malloc) will be returned.

Programmers will always see the illusion of consecutive (and yes, infinite :-) memory in a process's address space. This is what virtual memory provides to programmers.

Note that programmers (other than a few embedded systems) always see virtual memory. However, virtually consecutive memory could be mapped (in granularity of 'page' size, which is typically 4KB) in physical memory in arbitrary fashion. That mapping, you can't see, and mostly you don't need to understand it (except for very specific page-level optimizations).

What about this?

BYTE* data1 = new BYTE[size1];
BYTE* data2 = new BYTE[size2];

Sure, you can't say the relative address of data1 and data2. It's generally non-deterministic. It depends on heap manager (such as malloc, often new is just wrapped malloc) policies and current heap status when a request was made.

查看更多
SAY GOODBYE
6楼-- · 2020-04-09 06:54

If by your question you mean "Will successive (in time) new() operations return adjacent chunks of memory, with no gaps in between?", this old programmer will suggest, very politely, that you should not rely on it.

The only reason that question would come up was if you intended to walk a pointer "out" of one data object and "into" the next one. This is a really bad idea, since you have no guarantee that the next object in the address space is of anything remotely resembling the same type as the previous one.

查看更多
放荡不羁爱自由
7楼-- · 2020-04-09 06:55

Case 1: Using "new" to allocate an array, as in

int* foo = new int[10];

In this case, each element of foo will be in contiguous virtual memory.

Case 2: Using consecutive "new" operations non-atomically, as in

int* foo = new int;
int* bar = new int;

In this case, there is never a guarantee that the memory allocated between calls to "new" will be adjacent in virtual memory.

查看更多
登录 后发表回答