如何申请一段20M的 内存,用‘\0 ’ 填充。
  char * p;
  p = new char[2000000];
   然后怎么用 ‘\0’填充这段内存了?

解决方案 »

  1.   

    一般大内存块会用用内存的相关函数如HeapAlloc等一系列的函数来处理的
      

  2.   

    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.
      

  3.   

    char* pc = new char[20000000];
    memset(pc,0,20000000);
    pc[19999999] = 'a';
    cout << pc[19999999] << endl;
    正常,还以为会超出进程的默认堆会有问题呢,试了一下,没问题;以前看一个帖子说new在底层调用windows系统堆函数,而进程堆默认只有1MB,看来编译的时候堆给扩大了;
      

  4.   

    to  penter(净土) :
    能说说堆与栈的区别吗?我查了还有一个函数也行吧..:FillMemory
      

  5.   

    栈 : 主要是存储临时变量, 过程结束了 , 这些变量也就消亡了
          一般来说栈分配比较小堆:  随自己意愿来  采用 new/delete malloc/free 来分配
          一般比较大
      

  6.   

    to dxj221(green):  谢谢..这些区别我也了解.to approach():
      那我们最好使用VirtualAlloc() 这个函数来分配内存<特别是大内存>的吧..呵..:)
      

  7.   

    http://www.csdn.net/develop/article/19/19535.shtm
    看看这个...
      

  8.   

    VOID ZeroMemory(
      PVOID Destination, 
                     // address of block to fill with zeros
      DWORD Length   // size, in bytes, of block to fill with zeros
    );