gpt4 book ai didi

c++ - 尝试打印值时出现 bad_alloc 异常

转载 作者:行者123 更新时间:2023-11-28 01:12:31 25 4
gpt4 key购买 nike

我调试了我的 other problem返回到 MyMesh 构造函数。在这段代码中:

if (hollow) {
numTriangles = n*8;
triangles=new MyTriangle[numTriangles];
if (smooth) numSurfacePoints=n*8;
else numSurfacePoints=n*12;
surfacePoints=new SurfacePoint[numSurfacePoints];
}else {
numTriangles = n*4;
triangles=new MyTriangle[numTriangles];
if (smooth){
numSurfacePoints=n*4;
surfacePoints=new SurfacePoint[numSurfacePoints];
surfacePoints[n*4]=SurfacePoint(vector3(0,0,1),vector3(0,0,1));
surfacePoints[n*4+1]=SurfacePoint(vector3(0,0,-1),vector3(0,0,-1));
}else{
numSurfacePoints=n*6;
surfacePoints=new SurfacePoint[numSurfacePoints];
surfacePoints[n*6]=SurfacePoint(vector3(0,0,1),vector3(0,0,1));
surfacePoints[n*6+1]=SurfacePoint(vector3(0,0,-1),vector3(0,0,-1));
}
}

我正在确定网格所需的曲面点和三角形。 bool 值“hollow”和“smooth”表示,如果我需要在锥体上开一个洞,或者法线是否相同,但我认为这无关紧要。

问题是:如果 hollow==false,它会做错事,但不会崩溃,它甚至允许将值放入数组中,但是当我试图像这样计算它时:

for(int i=0;i<numSurfacePoints;i++){
std::cout<<"vertex "<<i<<"-> pos:"<<surfacePoints[i].pos.x<<" "<<
surfacePoints[i].pos.y<<" "<<surfacePoints[i].pos.z<<
" norm:"<<surfacePoints[i].norm.x<<" "<<surfacePoints[i].norm.y<<
" "<<surfacePoints[i].norm.z<<"\n";
}

当 i=0 时,它会抛出一个 bad_alloc 异常。

此外,有一段时间,上面的代码段向 operator new 抛出一个 bad_alloc,但这个问题自行解决了,但也许它是相关的。

谁能帮帮我?

最佳答案

然后你正在为 N 个表面点分配内存那么如何为第 N 点和第 N+1 点赋值?

请检查数组边界条件...

关于c++ - 尝试打印值时出现 bad_alloc 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1762566/

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