0

There are classes Field and Cell in my program and a few classes inherited from Cell. Main function works with Field, which has an array member containing pointers to cells of different classes. The cells are created in one method and I get Segfault when trying to access them in another methods. I feel that there must be a wrong array creation but have no ideas how to fix it. I tried to change array of pointers just to array of cells but got errors even in earlier stages of program.

To avoid putting here too much code: in main function first field is created as Field myfield; and then called functions for it: SetName, SetSize, CreateCells. Then I try to draw ConsoleDraw(&myfield); a field with next function:

void ConsoleDraw(Field *f)
{
    Cell* cell;

    for (int i=0; i<f->GetHeight(); i++)
    {
        for (int j=0; j<f->GetWidth(); j++)
            std::cout << f->GetCell(i,j)->GetType();

        std::cout << std::endl;
    }

    std::cout << std::endl;
}

Here is code for classes:
Field.h:

class Field
{
public:
    //some methods
    void ChangeCell(int i, int j, int type);
    void CreateCells();
    Cell* GetCell(int i, int j);

private:
    //some variables
    Cell*** cells;
};

Field.cpp:

void Field::CreateCells()
{
    cells = new Cell**[height];
    for (int i=0; i<height; i++)
    {
        cells[i] = new Cell*[width];

        for (int j=0; j<width; j++)
            ChangeCell(i,j,0);
    }

    for (int i=0; i<height; i++)
    for (int j=0; j<width; j++)
        cells[i][j]->SetNeighb();//operating with variables of single cell
}

void Field::ChangeCell(int i, int j, int type)
{
    if (cells[i][j]) delete cells[i][j];

    switch (type)
    {
        case 0:
            cells[i][j] = new Cell(0);
            break;
        case 1:
            cells[i][j] = new Cell(1);
            break;
        case 2:
        {
            cells[i][j] = new MovingCell;
            }
            break;
        case 3:
        {
            cells[i][j] = new CreatingCell;
            }
            break;
        case 4:
        {
            cells[i][j] = new KillingCell;
            }
            break;
        case 5:
        {
            cells[i][j] = new DyingCell;
            }
            break;
        default:
            if (type<10)
            {
                cells[i][j] = new DyingCell;
                cells[i][j]->SetChar(type-4);
            }
            else
            {
                cells[i][j] = new MovingCell;
                GetCell(i,j)->SetChar(type-10);
            }
    }

    cells[i][j]->SetCoordinates(j,i);
    cells[i][j]->SetOwner(this);
}

Cell* Field::GetCell(int i, int j)
{
    return cells[i][j];       //Here I got an error. 
}

I think there is no need in posting Cell class as all problems appear in operating with Field. Thank you in advance for any ideas of initialising that array correctly.

11
  • MovingCell mcell; cells[i][j] = &mcell; This makes cells[i][j] point to a local variable that is destroyed immediately afterwards. Voila - instant dangling pointer. Commented Dec 20, 2015 at 19:35
  • Another problem: new Cell*[width] creates an uninitialized array containing random garbage. But ChangeCell assumes the array contains NULL or valid pointers. Make it new Cell*[width]() (note a pair of parentheses), see if it helps; this causes array elements to be initialized to NULL Commented Dec 20, 2015 at 19:40
  • I tried to add parentheses but it didn't cause any changes. Commented Dec 20, 2015 at 19:44
  • By the way, couldn't it be the same problem with local variable for the whole array in CreateCells method? Commented Dec 20, 2015 at 19:47
  • What is this "local variable for the whole array" of which you speak? I don't see anything in CreateCells that could be reasonably described this way. Commented Dec 20, 2015 at 19:49

1 Answer 1

1

The line if (cells[i][j]) delete cells[i][j]; assumes that all cells have been allocated with new, but there are many cases in the switch statement below where cells are not allocated with new but rather point to shared locations. This looks unclean to me and can for sure cause segfaults.

Next error: You are assigning pointers to temporary objects in the default clause in the switch statement in ChangeCell. Any access to them will cause segfaults sooner or later. Any call to delete on them will cause segfaults.

Sign up to request clarification or add additional context in comments.

1 Comment

I repaired that things in switch. Thank you, it could cause further problems. But this error is still appearing in the same place, as there were created only cells of Cell type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.