Chinaunix首页 | 论坛 | 博客
  • 博客访问: 281391
  • 博文数量: 59
  • 博客积分: 1346
  • 博客等级: 中尉
  • 技术积分: 461
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-06 17:17
文章分类

全部博文(59)

文章存档

2012年(9)

2011年(50)

分类: C/C++

2012-05-10 16:50:29

#include
void mo(char **str)
{
 *str=(char *)malloc(8) ;
}

int main()
{
 char *str;
 char h[]="hello" ;
 str=NULL;
 mo(&str);
 //strcpy(str,h);
 strncpy(str,h,3);
 printf("%s",str);
 free(str);
 getchar(); 
}

//会打印hello,同时会出现内存泄露。 内存泄露就是要用free来释放


/*
void mo(char *str)
{
 str=(char *)malloc(100) ;
}

int main()
{
 char *str;
 char h[]="hello" ;
 str=NULL;
 mo(str);
 strcpy(str,h);
 printf("%s",str);
 getchar(); 
}
//不会有结果,因为str==NULL后导致malloc没有分配内存成功 ,这个程序去掉str==NULL就正常了
*/

阅读(2344) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~