创建动态数组时出现分段错误

Segmentation fault when creating dynamic arrray

我在学校工作,我的教授特别要求在不使用向量的情况下创建动态数组。我正在做的工作是一个在岛上玩的游戏,我需要使用动态数组来创建岛屿。

岛是一个 class,由一组单元格组成(这是另一个 class)。当我尝试将单元格插入岛阵列时,它会引发分段错误。

问题出在这个函数中:

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;

}

以下是其他相关函数:

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;
}

还有我的 class 头文件

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);
};

带有变量定义的头文件

/*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        "

Island class 中,您有一个指向指针 Cell** zone 的指针。您将不得不分配内存两次。一次用于 Cell*,然后用于 Cell。你将不得不做这样的事情(可能!):

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

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

完成后释放内存。 这是一个简单的整数示例:

    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;