返回信息流在VC里试验下面的小程序
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
#include <cstring>
int main()
{
char *ptr = (char*)malloc(0x10000);
strcpy(ptr, "hello world!");
cout << (void*)ptr << " " << ptr << endl;
char *qtr = (char*)realloc(ptr, 0x20000);
cout << (void*)qtr << " " << qtr << endl << endl;
cout << "now the old area becomes:" << endl;
cout << (void*)ptr << " ";
for(int i = 0; i < 12; i++)
cout << *ptr++;
cout << endl;
return 0;
}
程序本身很容易理解,但我不太明白的是最后一个for循环,在我的机器上单步调试的时候总是输出"铪铪铪铪铪铪",而运行的时候总是输出"hello world!".当然此时ptr指向的区域已经被释放了,这个结果并无太多意义,不过还是想知道为什么在两种状态下输出值会不同.
这是一条镜像帖。来源:北邮人论坛 / cpp / #38362同步于 2010/4/21
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
调试和运行时输出结果不同
disk
2010/4/21镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
【 在 disk 的大作中提到: 】
: 在VC里试验下面的小程序
: #include <iostream>
: using std::cout;
: ...................
没啥好奇怪的 debug模式的时候 会加入一些额外的检测代码 可能导致释放的内存被修改