c++ - C++ 访问冲突 - 可能是由于表?
问题描述
在主要功能中,我有以下内容。
int numRows = rowSequence.length() + 1;
int numCols = columnSequence.length() + 1;
int** twoDimTable = new int* [numRows];
for (int rowIndex = 0; rowIndex < numRows; rowIndex++)
{
twoDimTable[rowIndex] = new int [numCols];
}
//updating table
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
if (i == 0 || j == 0)
twoDimTable[i][j] = 0;
// when I start running my code I receive an unhandled exception right at the 'if'
// statement: Access violation writing location. I looked at other similar
// situations, but cannot seem to understand the specific issue
else if (rowSequence[i - 1] == columnSequence[j - 1])
twoDimTable[i][j] = twoDimTable[i - 1][j - 1] + 1;
else
twoDimTable[i][j] = max(twoDimTable[i - 1][j], twoDimTable[i][j - 1]);
}
}
解决方案
一个问题是你的 for 循环是错误的。numRows
并且numCols
不是有效的索引,因此它们不应包含在您的迭代中。
也就是说,而不是这样:
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
[...]
}
}
...你应该有这个:
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
[...]
}
}
推荐阅读
- c - 检查 docker alpine musl 版本
- java - 在 EditText 中键入时为文本添加下划线 - Android
- node.js - 无法创建反应应用,发现 1 个低严重性漏洞
- javascript - 如何从 React Native 中的 Reference 数据类型获取 Firebase Firestore 中的数据
- c++ - 输出的简单重定向与实现记录器
- asp.net-core - 为什么 Newtonsoft [JsonIgnore] 停止在 asp core mvc 中工作
- regex - 16位身份证的正则表达式如何写,规则如下?
- javascript - 为什么 obj.constructor.prototype 并不总是等于 obj.__proto__?
- javascript - 如何将用于 React SPA 的 Paypal Javascript SDK 与后端服务器验证集成?
- macos - IDE 不尊重 macOS 中的环境变量