iBacchus
iBacchus

Reputation: 175

Is the memory allocated by new operated consecutive?

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

Upvotes: 4

Views: 5552

Answers (7)

Vivek
Vivek

Reputation: 473

Physical memory is never contiguous its logical memory which is contiguous.

Upvotes: 0

John R. Strohm
John R. Strohm

Reputation: 7667

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.

Upvotes: 1

akappa
akappa

Reputation: 10490

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.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355187

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.

Upvotes: 7

minjang
minjang

Reputation: 9060

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.

Upvotes: 18

Chris Tonkinson
Chris Tonkinson

Reputation: 14469

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.

Upvotes: 4

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84189

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.

Upvotes: 2

Related Questions