Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1474104
  • 博文数量: 338
  • 博客积分: 2695
  • 博客等级: 少校
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-05 11:37
个人简介

小鱼儿游啊游啊。。。。

文章分类

全部博文(338)

文章存档

2019年(4)

2018年(8)

2017年(6)

2016年(10)

2015年(49)

2014年(48)

2013年(98)

2012年(115)

分类: LINUX

2015-10-21 20:14:12

程序1:


#include 
#include 
#include 

void getMemory(char *ptr)
{
        ptr = (char *)malloc(100);

        if(NULL == ptr)
        {
                printf("malloc memory failed\n");
                return ;
        }

        strcpy(ptr, "hello");
}

int main(int argc, const char *argv[])
{
        char *str = NULL;

        getMemory(str);
        printf("str = %s\n", str);

        free(str);

        return 0;
}


运行结果:str = (null)


程序2:


#include 
#include 
#include 

void getMemory(char *ptr)
{
        ptr = (char *)malloc(100);

        if(NULL == ptr)
        {
                printf("malloc memory failed\n");
                return ;
        }

        
}

int main(int argc, const char *argv[])
{
        char *str = NULL;
        
        getMemory(str);
        strcpy(str, "hello");
        printf("str = %s\n, str");


        free(str);

        return 0;
}


运行结果:Segmentation fault

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