I’m getting a segfault when I try to access a member of a class from within a method of that same class, which does not make sense to me at all.
I have the Tree class:
class Tree
{
public:
Coord* root;
Tree(int x, int y)
{
root = new Coord(x, y);
populateTree();
}
void populateTree()
{
queue<Coord*> nodes;
nodes.push(root);
while (nodes.size() > 0)
{
Coord* currnode = nodes.front();
nodes.pop();
if ( !(currnode->getValidMoves()) )
{
return;
}
else
{
for (int i = 0; i < MAX_CHILDREN_PER_COORD; i++)
{
if (currnode->children[i] != NULL)
{
nodes.push(currnode->children[i]);
}
}
}
}
}
…and the Coord class…
class Coord : public Loc
{
public:
Coord(int xPos, int yPos);
Coord* children[MAX_CHILDREN_PER_COORD];
bool getValidMoves();
bool operator==(Coord coord);
bool operator==(Loc loc);
};
Coord::Coord(int xPos, int yPos) : Loc(xPos, yPos) {}
bool Coord::getValidMoves()
{
//This line segfaults
Coord test = *this;
//Global boolean method. Checks found
if (!foundTrue())
{
for (int i = 0; i < MAX_CHILDREN_PER_COORD; i++)
{
//If the above segfaulting line is commented out, this is the first place that segfaults
int newX = x + knightPositions[i].x;
int newY = y + knightPositions[i].y;
if ( !(newX > GRID_X || newX < 0 || newY > GRID_Y || newY < 0) )
{
//knightPositions is a Loc array of length MAX_CHILDREN_PER_COORD
children[i] = new Coord(x + knightPositions[i].x, y + knightPositions[i].y);
//Global 2d array of ints. Gets checked by foundTrue()
found[x + knightPositions[i].x][y + knightPositions[i].y] = true;
}
}
return true;
}
else
{
return false;
}
//Otherwise, just leave it as a blank array
}
bool Coord::operator==(Coord coord)
{
return coord.x == x && coord.y == y;
}
bool Coord::operator==(Loc loc)
{
return loc.x == x && loc.y == y;
}
… and the Loc class, from which Coord inheirits…
class Loc
{
public:
int x, y;
//Constructor
Loc(int xPos, int yPos) : x(xPos), y(yPos) {}
};
The segfault occurs in Coord::getValidMoves() as indicated by the comments. If step through the code to that point, then make a watch for *this or x or this->x, I get a “Cannot access memory at 0xbaadf00d”
Why is this happening? Where have I messed up? I just don’t understand how trying to access *this in a method could possibly result in a segfault.
You need to initialise the elements of
Coord::children. They aren’t guaranteed to be NULL, so inpopulateTree(), when you do the null test on each child, you will get non-null children although they will not point to a validCoord. When they are popped off the queue, and you callgetValidMoves()on the invalidCoordyou’ll get the seg-fault.Change the
Coordconstructor to:(you’ll need to
#include <algorithm>forstd::fill.Note that the segfault occurs on the attempt to dereference
thisbecause that’s the first time you try to access the invalid memory.