使用memcpy填充后删除动态2d数组时中止(核心转储)

时间:2014-06-07 08:12:03

标签: c++ arrays string pointers

#include <iostream>
#include <cstring>

using namespace std;

const char* level1[23] =
{
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"X                                                               X",
"X  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     X",
"X  X K                          X            D               X  X",
"X  XXXXXXXXXXXXXXXXXXXXXX  XXXXXXXXX  XXXXXXXXXXXXXXXXXXXXX  X  X",
"X  X  X        X                  X                   XXXXX  X  X",
"X  X   XXXXX   X  XXXXXXXXXXXXXX  XXXXXXXX  XXXXXXXXXXX      X  X",
"X  X        X  X        D      X  X      X   X        X  XXXXX  X",
"X  X  XXXX  X  X  XXXXXXXXXX   X     X          X  XXXX      X  X",
"X        X        X   X  E X  XXXXXXXXX  XXXXXXXX  X  XXXXX  X  X",
"X  XXXXXXXXXXXXXXXX   D    X  XXK      X        X  X  X     KX  X",
"X  X     X     X KX   XXXXXX  XXXXXX  XXXXXXXX  X  X  X  XXXXXXDX",
"X  X  X  X  X  X  X           XXX  X            X  X  X  D   X  X",
"X  X  X  X  X  X  XXXXXXXXXXXXXXX  XXXXXXXXXXXXXX  X  XXXXX  X  X",
"X  X  X  X  X  X  X   X               X           KX  X      X  X",
"X  X  X  X  X  X  X   X  XXXXXXXXXXX  X  XXXXXXXXXXX  X  XXXXX  X",
"X  X  X  X  X  X  X   X  X     X  KX  X  X            X      X  X",
"X     X  X  X  X  X      X  X     XX  X  X     XXXXXXXXXXXXX X  X",
"X  X  X  X  X  X  X   X  X  XXXXXXXX  X  X      X     X      X  X",
"X  X  X     X     X   X  X         X  X  X   X     X         X  X",
"X  X  XXXXXXXXXXXXX   X  XXXXXXXX  X  X  X  XXXXXXXXXXXXXXXXXX  X",
"XS X                                                            X",
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
};

// Player structure
struct playerinfo
{
    int x;      // X position
    int y;      // Y position
    int keys;   // Number of keys the player has
};

// Current game info structure
struct gameinfo
{
    char** board;       // The maze array
    int maxx;           // Max size of the maze array X coordinate
    int maxy;           // Max size of the maze array X coordinate
    int level;          // The level of the maze
    playerinfo player;        // Link to player data struct
};

// Initializes the dynamic array that stores the maze
char** initboard(gameinfo game)
{
    char** matrix = new char*[game.maxx];

    for (int i=0; i<game.maxx; i++)
        matrix[i] = new char[game.maxy];

    return matrix;

}

void delboard(gameinfo game)
{
    for (int i=0;i<game.maxx;i++)
        delete [] game.board[i];
    delete [] game.board;
}

int main()
{
    gameinfo game;

    game.maxx=65;
    game.maxy=23;

    game.board = initboard(game);
    memcpy(game.board,level1,sizeof(level1));

    delboard(game);
}

核心转储发生在&#34; delete [] game.board[i];&#34;的第一步... ...仅在使用memcpy()行时才会发生。我想要做的是将c-string const的内容复制到动态2d数组。关键是游戏板会有多个级别可能不同的大小。我最初尝试game.board[0] = "contents of line 0" ... game.board[23] = "contents of line 23",但这给了编译器警告。奇怪的是,这在Windows下使用MinGW非常有效,但是我需要这个也能用于Linux。

3 个答案:

答案 0 :(得分:1)

这一行:

memcpy(game.board,level1,sizeof(level1));

是错误的,因为:

  1. 它会覆盖game.board的内容,char*的内容被level1的内容分配为initboard数组。

  2. 您正在编写未分配的内存。

  3. 您可以通过更改level1char** initboard(gameinfo game) { char** matrix = new char*[game.maxx]; for (int i=0; i<game.maxx; i++) { matrix[i] = new char[game.maxy]; // Copy the contents of `level1[i]` to `matrix[i]`. strpcy(matrix[i], level1[i]); } return matrix; } 的内容复制到正确的内存位置并删除上一行来解决问题。

    game.maxx

    更新(感谢@WhozCraig的敏锐眼光)

    您用于game.maxygame.maxx=23; // Not 65; game.maxy=65 // Not 23; 的值会相反。你需要:

    game.maxy+1

    然后,您需要在initboard中分配 matrix[i] = new char[game.maxy+1]; 个字符。

    matrix[i]

    否则,没有足够的空间来容纳{{1}}中的终止空字符。

答案 1 :(得分:1)

char** board;更改为vector<string> board;。摆脱initboarddelboard,并在main() go:

game.board = vector<string>( begin(level1), end(level1) );

您可能需要包含vectorstringiterator。您也可以删除maxxmaxy并直接从board阅读它们。

See it working ..看看这一切有多简单?

答案 2 :(得分:0)

您通过不调用new在堆栈中分配了这个,因此不需要在结尾处实际删除此数组。代码运行完毕后会处理好!您无法在堆栈上删除,或者您将从您无权访问的地址中删除,因此中止。