Adam Loska
Adam Loska

Reputation: 51

bad_alloc exception when trying to print the values

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?

Upvotes: 0

Views: 217

Answers (4)

denisenkom
denisenkom

Reputation: 414

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.

Upvotes: 0

Nicholaz
Nicholaz

Reputation: 1429

You are writing outside the allocated memory.

With

    numSurfacePoints=n*4;
    surfacePoints=new SurfacePoint[numSurfacePoints];

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

    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));

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.

Upvotes: 1

Ashish
Ashish

Reputation: 8529

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...

Upvotes: 2

xtofl
xtofl

Reputation: 41519

How much is n? That's where the amount of surfacePoints is calculated by...

Upvotes: 0

Related Questions