[英]Run-Time Check Failure #2 - Stack around the variable 'ap' was corrupted
I ran into above problem when I ran following little piece of code in visual C++ 2010 Express. 当我在Visual C ++ 2010 Express中运行少量代码时,遇到了上述问题。
When I used CodeBlocks, inner for
loop couldn't be executed completely because of line (A). 当我使用CodeBlocks时,由于(A)行,内部
for
循环无法完全执行。
Couldn't figure out why. 不知道为什么。 Thanks for help!
感谢帮助!
int main()
{
int* ap[10];
for(int j=0;j<10;j++){
*(ap+j) = new int[10];
for(int i=0;i<10;i++){
*((ap+j)+i) = *(ap+j)+i;//(A)
**((ap+j)+i) = j * 10 + i;
cout<<setw(6)<<**((ap+j)+i);
}
cout<<endl;
}
return 0;
}
This line 这条线
*((ap+j)+i) = *(ap+j)+i;//(A)
can be rewritten as 可以改写成
ap[j+i] = ap[j]+i;//(A)
and you can clearly see this can write out-of-bounds when j+i > 10. 并且您可以清楚地看到,当j + i> 10时,这可以写越界。
You probably meant ap[j][i]
or equivalently *(*(ap+j)+i)
. 您可能是指
ap[j][i]
或等效地*(*(ap+j)+i)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.