I am working on a class for an existing main program, and it is throwing a segfault on the delete [] line. I am not quite sure why it is doing this. I have asked the professor and I was told to reread the section about how pointers get corrupted. Any help would be appreciated! The relevant code is below.
From the main:
Point* v = new Point[nVertices];
for (int i = 0; i < nVertices; ++i)
in >> v[i].x >> v[i].y;
Asteroid aster1 (nVertices, v);
delete [] v;
The class section for asteroid’s constructor:
Asteroid::Asteroid(int nVertices, Point vertexPoints[]){
numVertices = nVertices;
for (int i = 0; i < numVertices; i++){
vertices[i] = vertexPoints[i];
}
}
And the .h section if that is significant:
class Asteroid{
int numVertices;
Point vertices[];
public:
//Attributes
Asteroid (int,Point*);
It looks very unlikely that the segfault is at the
delete[]line. Much more likely that it occurs in theAsteroidconstructor, as you are already attempting to access vertices before you’ve created the array which is supposed to contain them.