| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 830 人关注过本帖
标题:[求助]内存分配问题
取消只看楼主 加入收藏
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
快速回复:[求助]内存分配问题
数据加载中...
 
   



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

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