tags:

views:

290

answers:

7

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

+5  A: 

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.

James McNellis
So the corollary is: yes, you can treat them as consecutive (even if you really don't know if they are).
machielo
this is wrong -- you have no guarantee of ordering in or out of your process.
olliej
so you mean that the memory our program using is virtual memory? and the virtual memory is mapped from physical memory by OS(while this mapping mechanism is implemation-specific?)?
iBacchus
so...if all the program is using physical memory physically, why some one says memory newed-out is consecutive,while others says it's not?And I'm very much confused by deleting the inconsecutive memory...
iBacchus
Except a few embedded system, you can't simply play with physical memory.
minjang
minjang, I know that the memory is allocated by OS via new operater. But my confusion is I don't know whether this right memory I use by one new operation is consecutive or not. As far from the comments, it seems that I have to know much about the memory allocation of specific OS. And still I don't know whether I can assume the memory allocated by one new operation consecutive or not.
iBacchus
Simply, it is **consecutive**. Believe me. You don't mostly need to know the details of memory allocation. You don't even need to see the value of pointers.
minjang
Again (please see my answer), **virtual memory** gives (1) consecutive and (2) infinite illusion of memory. Yes, infinite is not true, 2^32/2^64 or 2^31 etc..
minjang
ok...thank you you all...this rises me to read some books about operating system...:)
iBacchus
Single allocations are contiguous, but the process address space often isn't. A common example are platforms where the OS has a good memory allocator, and where new() can forward directly. On those platforms you typically have a heap for small allocations, but larger blocks are allocated directly by the OS. As a result, after one small and one big allocation, you will have 2 disjoint areas of memory.
MSalters
+11  A: 
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.

minjang
Whoa there. If `new` fails, it throws `std::bad_alloc`. It **never** returns NULL.
rlbond
Sorry, confused. In old, Visual C++ implementation, it did return NULL, but now conforms C++ standard. http://msdn.microsoft.com/en-us/library/kftdy56f(VS.71).aspx
minjang
Infinite memory? I forget, what does that mean?
Michael Foukarakis
Infinite memory: more precisely, virtual memory gives an illusion of 2^32 or 2^64 address space. In this space, you generally don't need to consider the actual size of physical memory. Now, 32-bit address space is very limited, however, 64-bit space is technically *infinite*. (But, current x864-64 processors do not support full 64-bit virtual address space. Usually 46-bit or so.)
minjang
@rlbond - never say never. Besides older compilers (and VC6 is still pretty widely used), there's the `std::nothrow` overload for `new` that can return 0 for failure. Just bringing up a stupid bit of trivia.
Michael Burr
+2  A: 

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.

Nikolai N Fetissov
+3  A: 

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.

Chris
A: 

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.

akappa
A: 

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.

John R. Strohm
A: 

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

Vivek