Sign in to follow this  
Grain

Contiguous memory

Recommended Posts

If for example I allocate a large amount of memory, say 100MB or so. Will it be allocated contiguously; if not is there any thing I can to do to ensure contiguous allocation?

Share this post


Link to post
Share on other sites
If you allocate it in one call to operator new or malloc, it will be contiguous in your process' address space. This means it may not be contiguous in physical memory (in fact, it very likely won't be), but it will be transparent to your program.

Share this post


Link to post
Share on other sites
Yep, it'll be continuous, or the new/malloc call will fail.
(Think about it. It returns one pointer. If it wasn't pointing to a continuous memory area, you'd have no way of finding the allocated memory)

Share this post


Link to post
Share on other sites
Quote:
Original post by Grain
If for example I allocate a large amount of memory, say 100MB or so. Will it be allocated contiguously; if not is there any thing I can to do to ensure contiguous allocation?


if you mean raw uninitialized memory then:


void* buf = ::operator new(N); // or malloc
...
::operator delete(buf); // or free


note you can also choose which operator new/delete you want to use, just invoke it explicitly and it allocates/deallocates memory only does not invoke constructors/destructors.

also note std::vector is guaranteed to store elements in memory contiguously

[Edited by - snk_kid on July 8, 2005 11:04:53 AM]

Share this post


Link to post
Share on other sites
Kippesoep was correct. Memory is contiguous only in user address space. In OS kernel address space memory is usually divided into 4kb - 4mb pages (depends on os and cpu) just like filesystem sectors and it is usually fragmented.

Share this post


Link to post
Share on other sites
Ok, so lets say I use
char *MemoryChunk = new char[1048576]
I can assign other pointers to any address from
MemoryChunk to MemoryChunk + 1048575
with out worry.

Share this post


Link to post
Share on other sites
Quote:
Original post by Grain
Ok, so lets say I use
char *MemoryChunk = new char[1048576]
I can assign other pointers to any address from
MemoryChunk to MemoryChunk + 1048575
with out worry.


if the allocation succeded, yes, you can

Share this post


Link to post
Share on other sites
....of course, if you're planning to do some sort of pool allocator, you should keep alignment issues in mind. Otherwise, your code may get very slooooow (or crash, if you're on a MIPS machine).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this