void CTestDlg::OnButton1() 
{
    int i;     //int型最大只有32768!!!
    for(i=0;i<1048576;i++)
    {
test[i]= new SLeafNode;
    }}//想不出你的循环怎样退出for(int i=0;i<1024;i++)
  for(int j=0;j<1024,j++)
{
}

解决方案 »

  1.   

    另外,连续两个new出来的指针的地址不一定连续
    内存的分配是视内存当时的情况而定你可以分配一块整块的内存试试!
    应该没错是sizeof显示的数值
      

  2.   

    同样的问题:程序运行时:2056kbutton1点击后:58768kbutton2点击后:5212k
    不能理解。
      

  3.   

    我错了
    int型是四字节,最大2147483647
      

  4.   

    SLeafNode * test;    // 1024*1024//为1024*1024个结构分配内存
    void CTestDlg::OnButton1() 

    test= new SLeafNode[1024*1024];
    }
    试试!!
      

  5.   

    我把结构改为:
    struct SLeafNode 
    {
     int asdf;
    };运行结果竟然是:程序运行时:2076kbutton1点击后:56432kbutton2点击后:6360k怎么回事?????????
      

  6.   

    虽然delete,但系统可能并不立即收回所有空间,仅是做标记。
    猜测。
      

  7.   

    哥们,我觉得楼上那位说的有道理。劝你别再钻牛角尖了,我也照你的程序那样试了,结果跟你的差不多,可是我又看了processviewer里面的_Total的值,你也可以看看,不管你程序用的内存是多少,在程序启动之前和正常退出之后,系统的total内存基本上是持平的,所以你的程序不会有问题的。我也这么用过new 和 delete,每个节点差不多300 bytes,几十万的数据也没出现过问题,你也应该没事儿的! :)
      

  8.   

    分配内存的方式改为后
        test= new SLeafNode[1024*1024];按下button1前:
      working set 1328KB
      heap usage  116KB按下button1后:
      working set 1340KB
      heap usage  10360KB应该是对了!非常感谢!可是我在实际应用中,结构的分配与释放是动态进行的。无法进行一次性的整块分配,又该怎样解决这个问题?
      

  9.   

    无论是 new 操作符 还是 malloc 函数,真正消耗的内存(或是虚存)要比分配给程序的要大,因为系统要给这片内存附加上管理信息,象:本片大小、本片忙闲状态、上片/下片地址等等。
      

  10.   

    根据刚才(天下哪有那么多高手)的建议,我将程序作了以下修改:
    SLeafNode * test;void CTestDlg::OnButton1() 
    {
    test = new SLeafNode[1024*1024];
    }void CTestDlg::OnButton2() 
    {
        delete[] test;
    }void CTestDlg::OnButton3() 
    {

       fstream outfile;
       int i;   outfile.open("Framedata.dat",ios::out|ios::trunc);   i=sizeof (SLeafNode);
       outfile<<"size of structure is "<<i<<endl;
       outfile<<endl;   for(i=0;i<1024;i++)
       {
       outfile<<"Address of test["<<i<<"] is : "
                      <<(void *)&test[i]<<endl;
       outfile<<"m_uc_Status1                : "
                      <<(void *)(&(test[i].m_uc_Status1))<<endl;
       outfile<<"m_uc_Status2                : "
                      <<(void *)(&(test[i].m_uc_Status2))<<endl;
       outfile<<"e10                         : "
                      <<(void *)(&(test[i].e10))<<endl;
       outfile<<"e11                         : "
                      <<(void *)(&(test[i].e11))<<endl;
       outfile<<"m_Parent                    : "
                      <<(void *)(&(test[i].m_Parent))<<endl;
       outfile<<endl;
       }
       outfile.close();
    }
    结果如下:
    运行:
        working set:     1500KB
        heap  usage:      180KB按下button1:
        working set:     1512KB
        heap  usage:    10424KB
      
    按下button2:
        working set:     1508KB
        heap  usage:      180KB
    获得的内存地址见后;
    我又把那本古老而又经典的C++的书翻出来,看了看下面这段话,感到很惭愧(自己的C++学的太不扎实了)。“所谓堆对象是指在程序运行中根据需要随时可以建立或删除的对象。这种堆对象被创建在内存一些空闲的存储单元中,这些存储单元被称为堆。它们可以被创建的堆对象占有,也可通过删除堆对象而获得释放。创建或删除堆对象时,需要如下两个运算符:new delete”
    ----------引自《C++语言基础教程》192页--清华大学出版社
    堆--heap
    由上面的结果可以看到,当采用整块方式的内存分配时,内存开销的增加主要在对于堆的使用heap usage上,而working set中增加的很少。当释放后,增加的那部分堆的占用完全得到了释放。完全符合书中“用new创建的对象存储于堆中”的说法。我想当单个地为结构分配内存时,在堆里可能会有内存的碎片。所以堆的占用较大,达到了24712KB(见主贴)。而我不理解的是,既然用new创建的对象存储于堆中,为什么在单个地为结构分配内存时,working set的开销会增加很多,达到了30120KB?整块分配时却几乎没有增加?
    附:
    size of structure is 10Address of test[0] is : 0x008E0020
    m_uc_Status1                : 0x008E0020
    m_uc_Status2                : 0x008E0021
    e10                         : 0x008E0022
    e11                         : 0x008E0024
    m_Parent                    : 0x008E0026Address of test[1] is : 0x008E002A
    m_uc_Status1                : 0x008E002A
    m_uc_Status2                : 0x008E002B
    e10                         : 0x008E002C
    e11                         : 0x008E002E
    m_Parent                    : 0x008E0030Address of test[2] is : 0x008E0034
    m_uc_Status1                : 0x008E0034
    m_uc_Status2                : 0x008E0035
    e10                         : 0x008E0036
    e11                         : 0x008E0038
    m_Parent                    : 0x008E003AAddress of test[3] is : 0x008E003E
    m_uc_Status1                : 0x008E003E
    m_uc_Status2                : 0x008E003F
    e10                         : 0x008E0040
    e11                         : 0x008E0042
    m_Parent                    : 0x008E0044Address of test[4] is : 0x008E0048
    m_uc_Status1                : 0x008E0048
    m_uc_Status2                : 0x008E0049
    e10                         : 0x008E004A
    e11                         : 0x008E004C
    m_Parent                    : 0x008E004EAddress of test[5] is : 0x008E0052
    m_uc_Status1                : 0x008E0052
    m_uc_Status2                : 0x008E0053
    e10                         : 0x008E0054
    e11                         : 0x008E0056
    m_Parent                    : 0x008E0058
      

  11.   

    正像icessl所说的那样,在堆中分配内存时,在new所返回的地址
    前面实际上有不少字节的内存用来管理堆所需信息,在 Release Mode
    下面存了什么我不知道,我只知道在DebugMode 中它像一个双向链表的样子,
    其中有
    前一块,后一块碎片的管理信息的地址
    长度信息
    编号(不太确定,但挺像的)
    别的我就不知道了下面的例子中,
     NodeA,NodeB是两个结构
     sizeof(NodeA)=0x10
     sizeof(NodeB)=0x0c
     
     
    NodeA *pNodeA1=new NodeA;
    NodeA *pNodeA2=new NodeA;
    NodeB *pNodeB1=new NodeB;
    NodeA *pNodeAs=new NodeA[4];pNodeAs前面的内容:
    00790250  00 00 00 00 00 00 00 00  ........
    00790258  50 02 00 00 71 00 00 00  P...q...
    00790260  D0 02 79 00 00 00 00 00  ..y.....
    00790268  00 00 00 00 00 00 00 00  ........
    00790270  40 00 00 00 01 00 00 00  @.......
    00790278  1D 00 00 00 FD FD FD FD  ....pNodeB1前面的内容:
    007902C0  FD FD FD FD 00 00 00 00  ....
    007902C8  71 00 00 00 41 00 00 00  q...A...
    007902D0  10 03 79 00 60 02 79 00  ..y.`.y.
    007902D8  00 00 00 00 00 00 00 00  ........
    007902E0  0C 00 00 00 01 00 00 00  ........
    007902E8  1C 00 00 00 FD FD FD FD  ....pNodeA2前面的内容:
    00790300  00 00 00 00 00 00 00 00  ........
    00790308  41 00 00 00 41 00 00 00  A...A...
    00790310  50 03 79 00 D0 02 79 00  P.y...y.
    00790318  00 00 00 00 00 00 00 00  ........
    00790320  10 00 00 00 01 00 00 00  ........
    00790328  1B 00 00 00 FD FD FD FD  ....pNodeA1前面的内容:
    00790340  FD FD FD FD 00 00 00 00  ....
    00790348  41 00 00 00 41 00 00 00  A...A...
    00790350  90 03 79 00 10 03 79 00  ..y...y.
    00790358  00 00 00 00 00 00 00 00  ........
    00790360  10 00 00 00 01 00 00 00  ........
    00790368  1A 00 00 00 FD FD FD FD  ...写得很乱 请细看
      

  12.   

    分配很多小片内存时最好是自己来重载new, delete。
    碎片会少很多。