为了什么我需要使用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]带来的好处?