更改索引顺序 C++ 时的堆损坏
heap corruption when changing index order c++
double*** RGB = new double**[4];
for (int i = 0; i < 4; i++)
{
RGB[i] = new double*[6];
for (int j = 0; j < 6; j++)
{
RGB[i][j] = new double[4];
}
}
std::vector<int> columnIndex(24);
std::vector<int> rowIndex(24);
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
columnIndex[i*4 + j] = i;
rowIndex[i*4+ j] = j;
}
}
for (int n = 0; n < 24; n++)
{
for (int ch = 0; ch < 3; ch++)
{
....
/*RGB[rowIndex[n]][columnIndex[n]][ch] = median;*/ //old line working
RGB[ch][rowIndex[n]][columnIndex[n]] = median; //new line causing the heap corruption ... I think
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 6; j++)
{
delete[] RGB[i][j]; // crash is here when i and j are 0
}
delete[] RGB[i];
}
delete [] RGB;
更改前的旧代码工作正常。我怎么会注意到我的索引有问题,所以我不得不进行修复。修复后我崩溃了 - 我正在使用 Xunit。它说:
The thread 'Win64 Thread' (0x1f1c) has exited with code 0 (0x0).
HEAP[xunit.console.clr4.exe]: Heap block at 000000001D3C4820 modified at 000000001D3C4884 past requested size of 54
Windows has triggered a breakpoint in xunit.console.clr4.exe.
This may be due to a corruption of the heap, which indicates a bug in xunit.console.clr4.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while xunit.console.clr4.exe has focus.
使用我的调试器,我注意到当您将值存储到您使用columnIndex
时
columnIndex[i*4 + j] = i;
这给你
0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5
当你去使用它时
RGB[ch][rowIndex[n]][columnIndex[n]] = median;
[columnIndex[n]]
将被[0, 5]
但您只分配了 4 列
RGB[i][j] = new double[4];
这为您提供了有效的[0, 3]
indes,因此您将离开数组的末尾。
看起来您需要做的就是在columnIndex
和rowIndex
设置之间翻转i
和j
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 4; j++)
{
columnIndex[i * 4 + j] = j;
rowIndex[i * 4 + j] = i;
}
}
相关文章:
- 为什么会发生堆损坏
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 平均图像时图像损坏
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 为什么C中的通用链表中存储的数据已损坏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- C++双重释放或损坏(out)
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用全局声明的向量时,C++双重释放错误/损坏
- 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 删除字符串后C++检测到堆损坏
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 我可以写入关闭的套接字并强制纠正损坏的管道错误吗?
- 损坏的结构字符数组 - sqlite C++
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- zlib 膨胀在使用小缓冲区时会损坏
- 正在调试 malloc():新内存损坏
- 变量周围的堆栈'sortArray'已损坏