Chinaunix首页 | 论坛 | 博客
  • 博客访问: 464210
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: LINUX

2013-02-20 16:50:19

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

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

丫叩酱2013-04-08 13:32:14

丫叩酱确实,可能当时传错了,对不起大众啊,马上改过来

已改正,望多多指教

回复 | 举报

丫叩酱2013-04-08 13:22:03

zyx6a:貌似程序1,2是一样的?

确实,可能当时传错了,对不起大众啊,马上改过来

回复 | 举报

zyx6a2013-04-07 22:58:43

貌似程序1,2是一样的?