Contiguous memory
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 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.
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)
(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)
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]
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.
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.
Quote:Original post by Grain
Ok, so lets say I useI can assign other pointers to any address fromchar *MemoryChunk = new char[1048576]
with out worry.MemoryChunk to MemoryChunk + 1048575
if the allocation succeded, yes, you can
....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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement