Hi, i_tingfeng, you lost a ';'. :-) ---- Hi,youou, Are you sure that you will call 20MB block really? You may want to look in the Win32 Heap apis (HeapCreate(), ...) to create an extra heap for it. Or use the virtual memory apis (VirtualAlloc())If you don't need the whole block of 20MB at the same time, but have some data that grows over time up to 20MB, you should also look into VirtualAlloc() and SEH using PAGE_GUARD exceptions (example in the VirtualAlloc() docs in MSDN) . This allows you to *reserve* the address space for your 20MB block, but automatically *commit* only those address areas with RAM that are really need.
char* pc = new char[20000000]; memset(pc,0,20000000); pc[19999999] = 'a'; cout << pc[19999999] << endl; 正常,还以为会超出进程的默认堆会有问题呢,试了一下,没问题;以前看一个帖子说new在底层调用windows系统堆函数,而进程堆默认只有1MB,看来编译的时候堆给扩大了;
to penter(净土) : 能说说堆与栈的区别吗?我查了还有一个函数也行吧..:FillMemory
----
Hi,youou,
Are you sure that you will call 20MB block really?
You may want to look in the Win32 Heap apis (HeapCreate(), ...) to
create an extra heap for it. Or use the virtual memory apis
(VirtualAlloc())If you don't need the whole block of 20MB at the same time, but have
some data that grows over time up to 20MB, you should also look into
VirtualAlloc() and SEH using PAGE_GUARD exceptions (example in the
VirtualAlloc() docs in MSDN) . This allows you to *reserve* the
address space for your 20MB block, but automatically *commit* only
those address areas with RAM that are really need.
memset(pc,0,20000000);
pc[19999999] = 'a';
cout << pc[19999999] << endl;
正常,还以为会超出进程的默认堆会有问题呢,试了一下,没问题;以前看一个帖子说new在底层调用windows系统堆函数,而进程堆默认只有1MB,看来编译的时候堆给扩大了;
能说说堆与栈的区别吗?我查了还有一个函数也行吧..:FillMemory
一般来说栈分配比较小堆: 随自己意愿来 采用 new/delete malloc/free 来分配
一般比较大
那我们最好使用VirtualAlloc() 这个函数来分配内存<特别是大内存>的吧..呵..:)
看看这个...
PVOID Destination,
// address of block to fill with zeros
DWORD Length // size, in bytes, of block to fill with zeros
);