帮我看看这段c++代码,很短

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:48:51
#include<iostream.h>
#include<malloc.h>
#include<string.h>
void main()
{
char *s=(char *)malloc(100);
strcpy(s,"Hello");
free(s);
// cout<<s<<endl;
if(s!=NULL){strcpy(s,"HHHH");}
cout<<s<<endl;
}
双斜杠后面这一行,如果没有注释掉的话,能正常输出HHHH,如果注释掉的话,输出H吨吨,也就是为什么加了一行输出s 会使得最后一个输出变的正常了?

#include<iostream.h>
#include<malloc.h>
#include<string.h>
void main()
{
char *s=(char *)malloc(100);
strcpy(s,"Hello");
cout<<s<<endl;
free(s);
if(s!=NULL){strcpy(s,"HHHH");}
cout<<s<<endl;
}
好了,因为free(s)已经释放了s当然输不出,调一下就好