我想在我的基于 Windows 的软件中渲染 400 万个三角形,该软件是用 Visual Studio C++ 2010(以发布模式构建)编写的。当我渲染 390 万个三角形时,软件消耗的总 RAM 内存为 400MB。但是当我尝试渲染 400 万个三角形(仅多出 100K)时,系统给了我一个错误。
For Example:
Point *P = new (std::nothrow) Point[nb_triangles]; //==> "(std::nothrow)" is for catching the run time memory allocation error. (Point is X, Y, Z floats)
If(P == NULL)
message("System can't allocate this much memory."); // System gives me this error. It means the system can't reserve huge memory for this operation.
我必须为顶点、面法线、顶点法线等分配内存。
实际上我没有得到的是,我有 8 GB RAM 内存,(但在 32 位 XP windows = 3.2GB 内存),软件只保留 400MB,可用内存超过 1 GB,但是当我尝试再渲染 100K 三角形,它给了我一个错误。为什么它给我一个错误?因为它仍然有超过 1 GB 的可用 RAM 内存?
无论如何要解决这个问题,我怎样才能为我的应用程序分配所有可用内存?因为这个问题,我不得不在软件中做一个限制,只为渲染 390 万个三角形,这并不好。
我心中的另一个问题是,用于内存分配的 c++“new”运算符给了我错误,那么 c“malloc”运算符呢?“malloc”可以解决这个问题,这两者之间有什么区别吗?
请指导我。谢谢。
更新#1:
我已经尝试了很多,修改代码,消除内存泄漏等,但我无法分配超过400万的内存。不可能将我的整个代码更改为“向量”。我不能变成“向量”,我现在必须用“新”来坚持我自己的数据结构。以下是我要分配以呈现 1 个对象的指针。
P = new points[10000000]; // points is the class with 3 floats X, Y, Z;
N = new Norm[10000000]; // Norm is the class with 3 floats X, Y, Z;
V = new vNorm[10000000]; // vNorm is the class with 3 floats X, Y, Z;
T = new Tri[10000000]; // Tri is the class with 3 integers v1, v2, v3;