堆栈损坏,不知道是什么原因造成的
Stack Corruption, have no idea what's causing it
所以,为了有点事要做,我在C++中实现了AES算法。它的编译和链接非常好。不过,在我运行它的那一刻,VS2015报告称堆栈在变量"temp"周围已损坏。它向我展示了它发生的确切位置,但我在代码中看不到任何奇怪的东西:
void rotWord(Word &it)
{
Word temp;
for (int i = 0; i < 4; i++)
temp[(i - 1) % 4] = it[i];
for (int i = 0; i < 4; i++)
it[i] = temp[i];
}
顺便说一下,Word
被声明为typedef Byte Word[4]
,其中Byte
只是一个类。知道是什么导致了这里的堆栈损坏吗?如果需要,我可以发布完整的来源。
for (int i = 0; i < 4; i++)
temp[(i - 1) % 4] = it[i];
猜猜(0-1(%4是什么?
是-1。
在循环的第一次迭代中,i
为0,这将计算为:
temp[-1]=it[0];
将其更改为:
for (int i = 0; i < 4; i++)
temp[(i + 3) % 4] = it[i];
temp[(i - 1) % 4] = it[i];
对于i = 0
temp[((0 - 1) % 4]
temp[(-1) % 4]
temp[-1]
这是未定义的行为。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在Visual Studio中生成的匿名命名空间变量的损坏名称的数字部分是什么意思
- 安卓设备上二进制文件损坏的可能原因是什么
- 在同一个编译器(vc12)上进行编译时,是什么原因导致了损坏名称的差异
- 查找仅在性能测试下发生的堆损坏的最佳方法是什么?
- C++dll返回的字符串在C#调用程序中损坏,原因是什么
- 堆栈损坏,不知道是什么原因造成的