标题:[求助]内存分配问题
取消只看楼主
overcome
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-3-11
 问题点数:0 回复次数:2 
[求助]内存分配问题

以下两个程序输出的结果为何不一样:请指点一二

程序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;
}

搜索更多相关主题的帖子: 内存 pstr 分配问题 include 
2007-11-15 14:19
overcome
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-3-11
得分:0 

why?

2007-11-18 09:56
overcome
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-3-11
得分:0 

我说的是最后一句输出语句

2007-11-18 09:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-186170-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.780391 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved