在堆上分配最大内存
allocate maximum memory on heap
我想知道程序如何在抛出错误消息进行最终分配之前使用new[]来分配最大内存量,就像一样
#include <iostream>
using namespace std;
int main()
{
try
{
do
{
int i = new int;
throw sth;
.......
}while(true);
}
catch(Exception){
cerr<<"out of range"<<endl;
}
return 0;
}
像这样的东西。我真的被这个问题打动了,并为此工作了很长时间。如何做到这一点的示例算法也可能非常受欢迎。请帮帮我提前感谢
为什么不做这样的事情?
size_t s = 0;
char* p = nullptr;
while(p = new (std::nothrow) char[++s]) delete[] p;
std::cout << "Max successful heap allocation: " << s - 1 << " bytes." << std::endl;
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 类型总是使用其大小存储在内存中吗
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存