尝试打印值时发生 bad_alloc 异常
我已将我的其他问题调试回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));
}
}
我正在确定网格所需的表面点和三角形。布尔值“空心”和“平滑”表示,我是否需要在圆锥体上有一个孔,或者法线是否相同,但我认为这是无关紧要的。
问题是:如果空洞==假,它会做错事,但不会崩溃,它甚至允许将值放入数组中,但是当我尝试像这样计算它时:
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";
}
它抛出一个 bad_alloc 异常,就在 i=0 的时候。
另外,有一段时间,上面的代码段在运算符 new 上抛出了 bad_alloc ,但这个问题刚刚解决了自己,但也许它是相关的。
有人可以帮助我吗?
I've debugged my other problem back, to the MyMesh
constructor. In this code:
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));
}
}
I'm determining the neccessary SurfacePoints and Triangles for the mesh. The bools "hollow" and "smooth" indicates, if i need a hole in the cone, or if the normals are the same, but i think it's irrevelant.
The problem is: if hollow==false, it does something wrong, but doesn't crashes, it even allows to put the values in the arrays, but when I'm trying to cout it like this:
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";
}
it throws a bad_alloc exception, right when i=0.
additionally, there was a time, when the upper code segment threw a bad_alloc at the operator new, but that problem just solved itself, but maybe it's relevant.
can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在为 N 个表面点分配内存
那么如何为第 N 点和第 N+1 点赋值呢?
请检查数组范围外的条件...
You are allocating memory for N surface points then
so how can you assign value to N th and N+1 th point ?
Please put check for out of array bound conditions...
您正在分配的内存之外进行写入。
使用
有效的索引范围,您可以使用 [0 ... (n*4 - 1)] (例如,对于 n=10 您可以使用索引 [0..39] ),
但是您可以编写
超出该范围的内容(相同发生在平滑的情况下),即对于 n==10,您写入索引 40 和 41。
因此,您正在破坏堆(内存是“新”的来源)。根据内存布局(每次运行可能会有所不同),您会覆盖属于其他人(堆或程序的另一部分)的数据。根据布局,它会立即崩溃,或者为以后的崩溃或问题埋下种子。
在您的情况下,当您执行输出时,运行时库还将分配(调用 malloc 或 new)以获取其正在执行的操作的一些内存,并且堆系统注意到出现了问题(您覆盖了堆系统需要的一些数据)管理内存块)并抛出异常。
You are writing outside the allocated memory.
With
the valid index range you can use is [0 ... (n*4 - 1)] (e.g for n=10 you could use index [0..39] )
but then you write
which both go beyond that (same happens with the smooth case), i.e. for n==10 you write to index 40 and 41.
Thus you are corrupting the the heap (the memory are where the 'new's are coming from). Depending on the memory layout (which can vary from one run to the next) you overwrite data which belongs to someone else (either the heap, or another part of your program). Depending on layout it will crash right away, or lay the seed to a later crash or problem.
In your case, when you do the output, the runtime library will also allocate (call malloc or new) to get some memory for what it's doing and the heap system notices that something is wrong (you overwrote some data that the heap system needs to manage the memory chunks) and throws the exception.
n
是多少?这就是表面点数量的计算方式...How much is
n
? That's where the amount of surfacePoints is calculated by...您的堆似乎已损坏。检查您是否没有多次删除分配的指针。不访问数组的越界元素。不访问已删除的指针等。
It looks like your heap is corrupted. Check that you are not deleting allocated pointers more than once. Not accessing out-of-bounds elements of arrays. Not accessing deleted pointers, etc.