C++ 內存分配常見錯誤Stack corrupted.


BUG:Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.


Why?

#include <iostream>	

int main()
{
	char a[] = "hello";
	a[6] = 'A';
	std::cout << a;
	system("pause");
	return 0;
}

問題處在代碼的下一行。數組a,在STACK上內存分配成功,但是操作超過了內存的邊界,所以導致STACK corrupted

a[6] = 'A';

如果把a[6]修改爲a[0],問題就可以解決了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章