gpt4 book ai didi

c++ - 使用 memcpy 填充后删除动态二维数组时中止(核心转储)

转载 作者:行者123 更新时间:2023-11-28 02:46:59 25 4
gpt4 key购买 nike

<分区>

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

核心转储发生在“delete [] game.board[i];”的第一步......这只发生在 memcpy() 行用来。我想要做的是将 c 字符串 const 的内容复制到动态二维数组。关键是将有可能不同大小的多个级别作为 game.board 放入。我最初尝试过 game.board[0] = "contents of line 0" ... game.board[23] = "contents of line 23",但是给出了编译器警告。奇怪的是,这在 Windows 下使用 MinGW 时效果很好,但我需要它也能在 Linux 上使用。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com