C++ C++;访问冲突-可能是由于表?
在主功能中,我有以下功能。C++ C++;访问冲突-可能是由于表?,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]; } /
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]);
}
}
int numRows=rowSequence.length()+1;
int numCols=columnSequence.length()+1;
int**twoDimTable=newint*[numRows];
对于(int-rowIndex=0;rowIndex for(int i=0;i一个问题是for循环是错误的;numRows
和numCols
是无效的索引,因此它们不应该包含在迭代中
也就是说,与此相反:
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
[...]
}
}
for(int i=0;i一个问题是for循环是错误的;numRows
和numCols
是无效的索引,因此它们不应该包含在迭代中
也就是说,与此相反:
for (int i = 0; i <= numRows; i++)
{
for (int j = 0; j <= numCols; j++)
{
[...]
}
}
for(int i=0;我经常犯这个错误,您将了解到使用new
是一种代码味道too@HansPassant为什么,我真的想知道?@ C++中的XIIKH索引容器(包括C样式数组)通常从0开始索引,这意味着第th元素的大小超出范围range@M.M谢谢你的解释!经常犯这个错误,你就会知道使用new
是一种代码味道too@HansPassant为什么,我真的想知道?@ C++中的XIIKH索引容器(包括C样式数组)通常从0开始索引,这意味着第th元素的大小超出范围range@M.M谢谢你的解释!@StackDanny原始代码确实防止了这种情况(尽管你可能因为问题格式不好而错过了它)是的,对。你应该回滚@JeremyFriesner@StackDanny最初的代码确实防止了这种情况(尽管您可能因为问题格式不正确而错过了它)是的,对。您应该回滚@JeremyFriesner