爲了什麼我需要使用VirtualAlloc/VirtualAllocEx?我需要什麼來使用VirtualAlloc/VirtualAllocEx?
一個例子,我發現的一個案例 - 如果我分配了4 GB的虛擬內存,那麼如果我沒有使用它們全部,那麼我不花費物理內存,如果我調整我的陣列大小,我會不需要將新數據重新分配和複製到新陣列。
struct T_custom_allocator; // which using VirtualAllocEx()
std::vector<int, T_custom_allocator> vec;
vec.reserve(4*1024*1024*1024); // allocated virtual memory (physical memory is not used)
vec.resize(16384); // allocated 16KB of physical memory
// ...
vec.resize(32768); // allocated 32KB of physical memory
// (no need to copy of first 16 KB of data)
如果我使用的標準分配,我需要數據的拷貝,當我做調整:
std::vector<int> vec;
vec.resize(16384); // allocated 16KB of physical memory
// ...
vec.resize(32768); // allocated 32KB of physical memory
// and need to copy of first 16 KB of data
或用standatd分配器,我必須花4GB的物理內存:
std::vector<int> vec;
vec.reserve(4*1024*1024*1024); // allocated 4GB of physical memory
vec.resize(16384); // no need to do, except changing a local variable of size
// ...
vec.resize(32768); // no need to do, except changing a local variable of size
但是,爲什麼這比realloc()更好? http://www.cplusplus.com/reference/cstdlib/realloc/
是否有任何其他情況下使用VirtualAlloc [Ex]帶來的好處?