gpt4 book ai didi

c++ - CRT & new - 92 字节的 30 次调用给出 "bad_alloc nomem"

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:59:03 25 4
gpt4 key购买 nike

我正在为 C++ CRT DirectX 应用程序中 2000 多个对象的最近邻居编写一个动态八叉树。在调用新运算符 30 次后,程序会抛出“bad_alloc nomem”。我没有在其他任何地方使用 new。

我现在不知所措,以下是程序大纲以及我迄今为止为消除此错误所做的最大努力。即使在大量剪切之后仍然有很多 - 但请不要让它让您失望!

这是类结构:

template <typename T, int MaxObjPerNode>
class Octree
{...

public:
struct Node
{
int objcnt; // 0 == Node 4bytes
Node* parent; // 0 == Base 4bytes
D3DXVECTOR3 centre; //12bytes
Node* children[8]; //32bytes
Cube bounderies; //24bytes (Struct of 6 floats)
T* obj[MaxObjPerNode]; //4bytes *4

Node()
{
objcnt=0;
parent = 0;
centre = D3DXVECTOR3(0,0,0);
for (int t=0; t<8; t++)
children[t] = 0;
for (int t=0; t<MaxObjPerNode; t++)
obj[t] = 0;
}

Node* GiveBirth(int const Oct)
{
//...Sanity checks & intalisation
Node* NewNode = 0;
//_CrtMemState myBug;
NewNode = new Node(); // Nasty man sits here
//_CrtMemCheckpoint(&myBug);
NewNode->parent = this;
NewNode->centre = NewCentre;
//... rest of NewNode init.
}
//... Other functions sniped
}
private:
Node* base;
int StrtVecOff;
public:
Octree(float InitalCubeSize, T* FirstObj, long VectorOffset)
{
//D3DXVECTOR3* pTemp;
base = new Node();
StrtVecOff = VectorOffset; //Used to find Current Position in class T
base->objcnt = 1;
base->parent = 0;
base->obj[0] = FirstObj;
//... rest of base init.
}

Node* Add(T* Obj)
{
D3DXVECTOR3* Point = (D3DXVECTOR3*)(((long)(Obj))+StrtVecOff);

Node* InNode;
InNode = FindPlaceInTree(Point);
// FindPlaceInTree returns as Parent if Leaf has not been created from Node - So create leaf child
if (InNode->objcnt == 0)
{
int Oct = InNode->FindOctet(Point);
Node* ChildNode = InNode->GiveBirth(Oct);
ChildNode->obj[ChildNode->objcnt] = Obj;
ChildNode->objcnt +=1;
return ChildNode;
}
//...Other situations snipped
}
};

这是调用代码,

Octree<Mob,4> MyTree((float)(1<<13), &Bird[0],((long)&Bird[0].Pos - (long)&Bird[0]) );

for (int t=0;t<BirdsCount;t++)
MyTree.Add(&Bird[t]);

八叉树完美初始化,base* 正确指向有效数据。在 BANG 之前,对 new 的所有其他调用(30 * 92 字节)都没有问题。所以我进行了堆检查(在上面 GiveBirth 的注释代码处);通过 1:

-       myBug   {pBlockHeader=0x003d3208 lCounts=0x0012f938 lSizes=0x0012f94c ...}  _CrtMemState
+ pBlockHeader 0x003d3208
_CrtMemBlockHeader * //..snip - correct
- lCounts 0x0012f938 unsigned int [5]
[0] 0 unsigned int
[1] 2 unsigned int //First time past + base
[2] 45 unsigned int
[3] 0 unsigned int
[4] 0 unsigned int
- lSizes 0x0012f94c unsigned int [5]
[0] 0 unsigned int
[1] 184 unsigned int //Correct 2*92
[2] 8409 unsigned int
[3] 0 unsigned int
[4] 0 unsigned int
lHighWaterCount 12505 unsigned int
lTotalCount 18238 unsigned int

第 2-28 遍:实际上都是相同的

第 29 次:

-       myBug   {pBlockHeader=0x003d5c50 lCounts=0x0012f938 lSizes=0x0012f94c ...}  _CrtMemState
+ pBlockHeader 0x003d5c50 {pBlockHeaderNext=0x003d5bb8 pBlockHeaderPrev=0x00000000 szFileName=0x00000000 <Bad Ptr> ...} _CrtMemBlockHeader *
- lCounts 0x0012f938 unsigned int [5]
[0] 0 unsigned int
[1] 30 unsigned int
[2] 45 unsigned int
[3] 0 unsigned int
[4] 0 unsigned int
- lSizes 0x0012f94c unsigned int [5]
[0] 0 unsigned int
[1] 2760 unsigned int
[2] 8409 unsigned int
[3] 0 unsigned int
[4] 0 unsigned int
lHighWaterCount 12505 unsigned int
lTotalCount 20814 unsigned int

没有通过 30 :(我知道我有超过 2760 字节的空闲空间(我不再使用 vic 20!)。

我看了很多“相关问题”,仍然没有喜悦。欢迎任何建议

最佳答案

new 因任何原因失败时会抛出内存异常。它不一定是内存不足的情况。不要被愚弄了。某些东西在构造函数中抛出异常。

关于c++ - CRT & new - 92 字节的 30 次调用给出 "bad_alloc nomem",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5276168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com