2D阵列试图填充时失败
2d-Array fails when trying to populate
我正在编写一个最终将解决8年拼图的课程。我添加了一个名为pupulate的测试方法,以确保正确和动态分配内存正确创建2D数组,但是当该方法称为程序崩溃时,当我调试以下Visual Studio错误时,请弹出:
。Exception thrown at 0x01281DFB in 8Queen.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD.
If there is a handler for this exception, the program may be safely continued.
我的构造函数:
Queen::Queen(int s)
{
size = s;
board = new int *[size];
for (int i = 0; i < size; ++i)
board[size] = new int[size];
}
我的填充方法:
void Queen::populate()
{
for (int i = 0; i < size; ++i) // for each row
for (int j = 0; j < size; ++j) // for each column
board[i][j] = 1;
}
我的解构:
Queen::~Queen()
{
for (int i = 0; i < size; i++)
delete[] board[i];
delete[] board;
}
我的主要:
int main()
{
fm::Queen board(10);
board.populate();
board.printBoard();
return 0;
}
在您的构造函数中,您有以下行:
board[size] = new int[size];
可能,您希望这是
board[i] = new int[size];
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 在c++中用vector填充一个简单的动态数组
- 如何使用用户输入在C++中正确填充2D数组
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 如何找到大小'x'数组是否完全填充,在C++?
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 通过for循环使用用户输入填充列表
- 2D阵列试图填充时失败
- 根 - 填充失败分支:内存驻留树
- 尝试在类成员初始化中使用 vector 的填充构造函数失败。怎么了?