0

I'm sitting in front of a problem where I don't know the solution. In the end im always getting an segmentation fault.

I've created a "Map"-class where I store the following informations and this Map is stored as a Vector inside the Gameclass

class Map
{
  private:
    unsigned int map_id_;
    unsigned int map_width_;
    unsigned int map_height_;
    std::vector< std::vector< int > > fields_;
    Game *game_;

class Game
{
  private:
    ...
    std::vector< Map* > *v_map_;
    ...


std::vector< Map* > Game::getMapVector()
{
  return *v_map_;
}

Inside the Game-Class I fill up the informations, including the fields vector.

  for(int i = 0; i < height; i++)
  {
    std::vector< int > row;

    for (int j = 0; j < width; j++)
    {
      is.read(reinterpret_cast<char*>(&fields), 1);
      counter = counter - 1;
      row.push_back(int(fields));
    }

    fields_vector.push_back(row);
  }

If i try to output my map direct in the readin-process everything works perfect.

  for (int i = 0; i < height; i++)
  {
    for (int j = 0; j < width; j++)
    {
      std::cout << fields_vector[i][j];
    }
    std::cout << std::endl;
  }

Until now everything is ok. But now I'm getting the segmentationfault if I call it with an for-loop inside the auto iterator.

If I try it to output only one element with std::cout << (*it)->getMapFields()[1][1] inside the first for-loop i get the correct output on this position.

for(auto it = std::begin(game.getMapVector()); it != std::end(game.getMapVector()); it++)
{
  std::cout << "ID: " << (*it)->getMapId()
    << ", width: " << (*it)->getMapWidth()
    << ", height: " << (*it)->getMapHeight() << std::endl;


  for (int i = 0; i < (*it)->getMapHeight(); i++)
  {
    for (int j = 0; j < (*it)->getMapWidth(); j++)
    {
      std::cout << (*it)->getMapFields()[i][j];
    }
    std::cout << std::endl;
  }
}

Is there something i didn't see? Maybe an for-loop inside an auto iterator isn't possible?

Thanks in advance for helping, Philipp

1
  • Are you sure it's getMapFields()[i][j] and not getMapFields()[j][i] ? Commented Jun 2, 2014 at 8:40

1 Answer 1

5

One mistake is:

std::vector< Map* > Game::getMapVector()

returns a new std::vector<Map*> instance. This means that the begin and end iterators used in the following for loop refer to different std::vector instances:

for(auto it = std::begin(game.getMapVector()); 
   it != std::end(game.getMapVector());
   it++)

Possible solutions:

  • store the result of getMapVector() to a local variable and use it for iteration
  • change getMapVector() to return a (const) reference to the std::vector<Map*>
Sign up to request clarification or add additional context in comments.

2 Comments

In constructor of Game-class I'm creating the map-vector with v_map_ = new vector< Map* >; so it should be always the same Vector. What I don't understand is why I can get some Informations out of the vector with std::cout << (*it)->getMapFields()[1][1] but not with an forloop where [1][1] is replaced with [i][j]. Also I get an segmentation fault if i put std::cout << (*it)->getMapFields()[1][1] inside the second for-loop. Normally it should output always the same values, but I get segfaults.
@uskrd, getMapVector() will be a COPY of the same std::vector. The for loop, as it stands, will eventually result in dereferencing a past-the-end-iterator causing undefined behaviour.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.