Run-Time Check Failure #2 Stack around the variable ‘xxx’ was corrupted

在改别人代码时,运行报错:

Run-Time Check Failure #2 Stack around the variable ‘buffer’ was corrupted

这表明你对某变量的赋值操作,超出了其内存范围。

例,在我的代码中大致是这样的:

char buffer[10];
sprintf(buffer, "D:\\user\\test\\exp10.epoch03.autosave");

很明显,这里我给buffer赋值的字符串长度超出了其定义时分配的内存范围。

找到问题后突然感觉……真是好白痴的问题啊~
不过刚看到时还是挺没有头绪的,尤其是出问题的这个变量还用了buffer这种大众化的名字……
而且这种运行时问题是不会提示哪行出错的,茫茫多代码啊!这回充分体会到步进式debug是多么的重要了。

另外,在网上查看相关问题,几乎都是char[]+sprintf引起的,以后看到可以快速定位了。

相关问题引用:

Run-Time Check Failure #2 - Stack around the variable — was corrupted
奇怪的Stack around the variable was corrupted现象解析 以及 sprintf 的奇怪现象?谁能解释?

posted @ 2014-10-25 14:28  MindProbe  阅读(994)  评论(0编辑  收藏  举报