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++)
{
}
{
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++)
{
}
解决方案 »
- vs2010 c++ ADO 连接 oracle 11G 的问题
- 关于 socket的问题 (还是那个帖子)
- [简单]将常用的用点分开的IP地址转换为long类型的IP地址的函数
- 王艳平windows网络与通信程序设计代码调试求助
- 如何更改系统menu的背景和前景色??????
- 怎么把CString转化为_variant_t?
- 怎样用VC++执行SQL Server Query Analyzer的*.sql文件
- 想知道是否vc++下也有turboc下那样的tcc.exe编译命令!
- 本专业考到程序员有什么用?想做项目经理要经过什么考试?
- winform show()函数调用子窗体的问题
- 是没高手还是讨厌我(我的问题没人看)
- 难道今天这里没有高手吗! 我的问题很难吗!
内存的分配是视内存当时的情况而定你可以分配一块整块的内存试试!
应该没错是sizeof显示的数值
不能理解。
int型是四字节,最大2147483647
void CTestDlg::OnButton1()
{
test= new SLeafNode[1024*1024];
}
试试!!
struct SLeafNode
{
int asdf;
};运行结果竟然是:程序运行时:2076kbutton1点击后:56432kbutton2点击后:6360k怎么回事?????????
猜测。
test= new SLeafNode[1024*1024];按下button1前:
working set 1328KB
heap usage 116KB按下button1后:
working set 1340KB
heap usage 10360KB应该是对了!非常感谢!可是我在实际应用中,结构的分配与释放是动态进行的。无法进行一次性的整块分配,又该怎样解决这个问题?
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
前面实际上有不少字节的内存用来管理堆所需信息,在 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 ...写得很乱 请细看
碎片会少很多。