[求助]内存分配问题
以下两个程序输出的结果为何不一样:请指点一二
程序1:
#include<iostream.h>
#include<string.h>
#include<malloc.h>
void main()
{
char *pstr;
pstr=(char *)malloc(100);
strcpy(pstr,"hello");
//cout<<pstr<<endl;
free(pstr);
if(pstr!=NULL)
{
strcpy(pstr,"world");
}
cout<<pstr<<endl;
}
程序2:
#include<iostream.h>
#include<string.h>
#include<malloc.h>
void main()
{
char *pstr;
pstr=(char *)malloc(100);
strcpy(pstr,"hello");
cout<<pstr<<endl;
free(pstr);
if(pstr!=NULL)
{
strcpy(pstr,"world");
}
cout<<pstr<<endl;
}