Home > Enterprise >  Segmentation fault when creating dynamic arrray
Segmentation fault when creating dynamic arrray

Time:01-17

I'm doing work for school and my professor specifically asks for a dynamic array created without using a vector. The work I'm doing is a game that is played on an island and I need to use the dynamic array to create the island.

The island is a class that consists of an array of cells (which is another class). When I'm trying to insert the cells into the island array it throws a segmentation fault.

The problem is in this function:

Island* Island::Create(int cols, int rows) { // Static function
    auto* map = new Island;
    map->cols = cols;
    map->rows = rows;

    for (int i = 0; i < cols * rows;   i)
        map->zone[i] = Cell::Create(); //Segmentation fault here (When assigning cell to zone[i])

    return map;

}

Here are other relevant functions:

Island::Island(){
    cols = 0;
    rows = 0;
    zone = nullptr;
}

Cell::Cell() {
    type = "undef";
    building = nullptr;
    trees = 0;
}

Cell* Cell::Create(const string& type) {
    Cell* zone = new Cell;

    if (type == undef)
        switch ( rand() % 6   1) {

            case 1:
                zone->type = mnt;
                break;

            case 2:
                zone->type = dsr;
                break;

            case 3:
                zone->type = pas;
                break;

            case 4:
                zone->type = flr;
                zone->trees = 20;
                zone->trees  = rand() % 20;
                break;

            case 5:
                zone->type = pnt;
                break;

            case 6:
                zone->type = rad;
                break;

        }
    else zone->type = type;

    zone->building = Building::Create("undef");
    return zone;
}

And my class header file

class Island {
    int cols, rows;
    Cell** zone;

public:
    Island();
    ~Island();
    Island* Create(int cols, int rows);
};

class Cell {
private:
    string type;
    Building* building;
    vector <Worker*> worker_list;
    int trees;

public:
    Cell();
    ~Cell();
    Cell* Create(const string& type);
};

Header file with variable definitions

/*I know this is C, I just did this for convenience
  and will change later.
  The spaces are for correct formatting when displaying the island
  in the console.
*/
#define undef "             "
#define minaf "minaf        "
#define minac "minac        "
#define central "central      "
#define bat "bat          "
#define fund "fund         "
#define filt "filt         "
#define serr "serr         "

#define mnt "mnt        "
#define flr "flr        "
#define pnt "pnt        "
#define dsr "dsr        "
#define pas "pas        "
#define rad "rad        "

CodePudding user response:

In the Island class, you have a pointer to pointer Cell** zone. You will have to allocate memory twice. Once for Cell* and then for Cell. You will have to do something like this (probably!):

map->zone = new Cell*[rows];

for (int i = 0; i < rows;   i)
        map->zone[i] = new Cell[cols];

Then free the memory when done. Here's a simple example with integers:

    int** array2d;
    int rows = 2, cols = 3;
    array2d = new int*[rows];

    for (int i = 0; i < rows; i  )
    {
        array2d[i] = new int[cols];
    }

    for (int i = 0; i < rows; i  )
    {
        for (int j = 0; j < cols; j  )
        {
            array2d[i][j] = i   j;
        }
    }

    for (int i = 0; i < rows; i  )
    {
        for (int j = 0; j < cols; j  )
        {
            std::cout << array2d[i][j] << " ";
        }
        std::cout << std::endl;
    }

    // free the memory
    for (int i = 0; i < rows; i  )
    {
        delete[] array2d[i];
    }
    delete[] array2d;
  •  Tags:  
  • Related