zaloo
zaloo

Reputation: 929

Segfault on copy constructor for string

I'm getting a segfault on a line where I'm creating a c++ string with the copy constructor. I've looked at some of the similar issues, but they're all due to passing in a bad c++ string object. I'm just passing in a raw string, so I'm not sure what my issue is. I'll paste the relevant snippets of code (it's taken from several different files, so it might look a bit jumbled). The segfault occurs in the 4th line of the default constructor for the Species class.

Species::Species(string _type) {
    program_length = 0;
    cout << _type << " 1\n";
    cout << type << " 2\n";
    type = string(_type);
}

Grid::Grid(int _width, int _height) {
    *wall = Species("wall");
    *empty = Species("empty");
    turn_number = 0;
    width = _width;
    height = _height;
    for(int a= 0; a < 100; a++)
        for(int b = 0; b< 100; b++) {
            Creature empty_creature = Creature(*empty,a,b,NORTH,this);
            (Grid::map)[a][b] = empty_creature;
        }
}

int main() {
    Grid world = Grid(8,8);
}

class Grid {
protected:
    Creature map[100][100];
    int width,height;
    int turn_number;
    Species *empty;
    Species *wall;
public:
    Grid();
    Grid(int _width, int _height);
    void addCreature(Species &_species, int x, int y, Direction orientation);
    void addWall(int x, int y);
    void takeTurn();
    void infect(int x, int y, Direction orientation, Species &_species);
    void hop(int x, int y, Direction orientation);
    bool ifWall(int x, int y, Direction orientation);
    bool ifEnemy(int x, int y, Direction orientation, Species &_species);
    bool ifEmpty(int x, int y, Direction orientation);
    void print();
};

class Species {
    protected:
    int program_length;
    string program[100];
    string type;
    public:
    species(string _type);
    void addInstruction(string instruction);
    bool isWall();
    bool isEmpty();
    bool isEnemy(Species _enemy);
    string instructionAt(int index);
    string getType();
};

Here's my updated code after I removed pointers from wall and empty. I'm now getting a strange error ("error: field wall is not a member of Grid"):

Grid::Grid(int _width, int _height) {
    (Grid::wall) = Species("wall");
        (Grid::empty) = Species("empty");
        cout << (*wall).getType() << "\n";
        turn_number = 0;
        width = _width;
        height = _height;
        for(int a= 0; a < 100; a++)
            for(int b = 0; b< 100; b++) {
                Creature empty_creature = Creature(Grid::empty,a,b,NORTH,this);
                (Grid::map)[a][b] = empty_creature;
            }
}

class Grid {
protected:
    Creature map[100][100];
    int width,height;
    int turn_number;
    Species empty;
    Species wall;
public:
    Grid();
    Grid(int _width, int _height);
    void addCreature(Species &_species, int x, int y, Direction orientation);
    void addWall(int x, int y);
    void takeTurn();
    void infect(int x, int y, Direction orientation, Species &_species);
    void hop(int x, int y, Direction orientation);
    bool ifWall(int x, int y, Direction orientation);
    bool ifEnemy(int x, int y, Direction orientation, Species &_species);
    bool ifEmpty(int x, int y, Direction orientation);
    void print();
};

Upvotes: 0

Views: 1115

Answers (1)

juanchopanza
juanchopanza

Reputation: 227518

The most likely cause of the problem is the Species pointer data members that you are de-referencing without initializing:

Grid::Grid(int _width, int _height) {
  *wall = Species("wall");  // wall not initialized. What does it point to?
  *empty = Species("empty"); // likewise for empty

Which begs the question: do you need them to be pointers anyway? (hint: most likely not)

When not using pointers, you could easily initialize the data members in the constructor's initialization list:

Grid::Grid(int _width, int _height) 
: width(_width), height(_height), turn_number(0), wall("wall"), empty("empty") 
{ 
  ....
} 

Upvotes: 3

Related Questions