简体   繁体   English

运行时检查失败#2-变量'ap'周围的堆栈已损坏

[英]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.

相关问题 运行时检查失败#2-变量“ z”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted 运行时检查失败2:变量&#39;expression&#39;周围的堆栈已损坏 - Run-Time Check Failure #2: Stack around the variable 'expression' was corrupted 运行时检查失败 #2 - 变量“normalIndex”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted 错误:运行时检查失败 #2 - 变量周围的堆栈已损坏 - ERROR: run-time check failure #2 - stack around the variable was corrupted 运行时检查失败#2-变量&#39;projectAverage&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'projectAverage' was corrupted 运行时检查失败#2-围绕变量&#39;&#39;的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable '' was corrupted 运行时检查失败#2-变量&#39;seqA&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'seqA' was corrupted 运行时检查失败#2-变量&#39;k&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'k' was corrupted 运行时检查失败 #2 - 变量“板”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'board' was corrupted 运行时检查失败 #2 - 变量“应用程序”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'application' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM