new 多大会失败。。。。

hlyces 2011-07-29 12:25:12
cout << "new\n";
char * p = new char[100000000000];
if (NULL == p)
{
cout << "new wrong !\n";
}

cout << "delete\n";
delete [] p;
p = NULL;

----------------------------------------------------------------
这都不会返回null,我表示无语。
虚拟内存+内存也不够100G啊,完全没这么大啊。。。
为毛还能申请成功呢。
...全文
121 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
至善者善之敌 2011-07-29
  • 打赏
  • 举报
回复
貌似申请这么大会有问题吧!你编译不错错
VS2005
Error 2 error C2148: total size of array must not exceed 0x7fffffff bytes c:\documents and settings\administrator\桌面\mfc程序\vs2005\vs2005.cpp 26
delphiwcdj 2011-07-29
  • 打赏
  • 举报
回复

#include <iostream>

int main()
{
size_t *p;
try
{
do
{
p = new size_t[1024*1024*500];
cout<<"Allocating memory..."<<p<<endl;
if(p == NULL)
{
break;
}
}while(p);
}
catch(...)
{
cout<<endl<<"catch..."<<endl;
}
cout<<"Allocating OK!....."<<endl;

return 0;
}

懒得打字 2011-07-29
  • 打赏
  • 举报
回复
这个例子分配了1PB的空间,new直接抛出异常,g++ 4.4

/*
* main.cpp
*
* Created on: Jul 29, 2011
* Author: tyj
*/
#include <iostream>
#include <cstdlib>

using namespace std;

void my_new_handler()
{
cerr << "Unable to satisfy request for memory\n";
abort();
}

int main(int argc, char* argv[])
{
cout << "new\n";
set_new_handler(my_new_handler);
char *p = new char[1000000000000000L];
if (NULL == p)
{
cout << "new wrong !\n";
}
p[0L] = 'a';
p[99999999999L] = 'b';
cout << p[0L] << p[99999999999L] << endl;
cout << "delete\n";
delete [] p;
p = NULL;
return 0;
}
懒得打字 2011-07-29
  • 打赏
  • 举报
回复
这个例子分配了100T的空间,一读写就失败了,g++ 4.4
/*
* main.cpp
*
* Created on: Jul 29, 2011
* Author: tyj
*/
#include <iostream>
#include <cstdlib>

using namespace std;

void my_new_handler()
{
cerr << "Unable to satisfy request for memory\n";
abort();
}

int main(int argc, char* argv[])
{
cout << "new\n";
// set_new_handler(my_new_handler);
char *p = new char[100000000000000L];
if (NULL == p)
{
cout << "new wrong !\n";
}
p[0L] = 'a';
p[99999999999L] = 'b';
cout << p[0L] << p[99999999999L] << endl;
cout << "delete\n";
delete [] p;
p = NULL;
return 0;
}
delphiwcdj 2011-07-29
  • 打赏
  • 举报
回复
测试下,会有MemoryException

cout << "new\n";
char * p = NULL;
while (1)
{
p = new char[1024*1024*500];// 500M
if (NULL == p)
{
cout << "new wrong !\n";
break;
}
}

cout << "delete\n";
delete [] p;
p = NULL;
懒得打字 2011-07-29
  • 打赏
  • 举报
回复
话说,new关键字只有在很老的编译器上才会返回NULL,因为那个时候还没有异常处理机制。现在的new如果分配失败会调用的new_handler,而new_handler默认会抛出std::bad_alloc异常,当然也可以使用set_new_handler改写行为
hlyces 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 demon__hunter 的回复:]
因为此时木有提交物理内存。
[/Quote]
什么意思~~~
hlyces 2011-07-29
  • 打赏
  • 举报
回复
那怎么知道申请失败。。。。
都不=NULL
机智的呆呆 2011-07-29
  • 打赏
  • 举报
回复
因为此时木有提交物理内存。
懒得打字 2011-07-29
  • 打赏
  • 举报
回复
只是先做个标记而已,当你第一次对那个空间读写的时候就失败了……
hlyces 2011-07-29
  • 打赏
  • 举报
回复
最后得出结论
在mfc下是不会得到NULL的。。。。
只要包含了afx.h..............
赵4老师 2011-07-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 babilife 的回复:]
貌似申请这么大会有问题吧!你编译不错错
VS2005
Error 2 error C2148: total size of array must not exceed 0x7fffffff bytes c:\documents and settings\administrator\桌面\mfc程序\vs2005\vs2005.cpp 26
[/Quote]
人家可能是64位系统。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧