首页 > 其他 > 详细

Getmemory问题

时间:2019-05-31 10:41:57      阅读:217      评论:0      收藏:0      [点我收藏+]

摘自:https://blog.csdn.net/zzl_python/article/details/83502614

 

#include <iostream>
/************************************************************************/
/* 1、改变形参值,不会影响到实参,因此在Test1中GetMemory返回之后,str依然为NULL */
/* 2、strcpy不安全                                                      */
/* 3、malloc没有free内存泄露                                              */
/************************************************************************/
void GetMemory1(char *p)
{
    p = (char *)malloc(100);
}
void Test1(void)
{
    char *str = NULL;
    GetMemory1(str);
    strcpy(str, "hello world");  //这里str为NULL
    printf(str);
}


/************************************************************************/
/* 1、返回局部变量的指针,GetMemory返回时p已无效                            */
/************************************************************************/
char *GetMemory2(void)
{
    char p[] = "hello world";
    return p;
}
void Test2(void)
{
    char *str = NULL;
    str = GetMemory2();
    printf(str);
}


/************************************************************************/
/* 1、字面值存储在全局静态存储区,因此程序正常运行                            */
/* 2、设计概念有问题,并没有分配内存,始终返回同一个地址                       */
/* 3、c++11 会报错const char []”转换为“char *                             */
/*   return const_cast<char *>("hello world");                            */
/************************************************************************/
char *GetMemory3(void)
{
    return "hello world";
}
void Test3(void)
{
    char *str = NULL;
    str = GetMemory3();
    printf(str);
}


/************************************************************************/
/* 1、运行结果正确,形参传二级指针,实际是str的地址                          */
/* 2、malloc之后需要判断是否成功                                          */
/* 2、strcpy不安全                                                       */
/* 3、malloc没有free内存泄露                                              */
/************************************************************************/
void GetMemory4(char **p, int num)
{
    *p = (char *)malloc(num);
}

void Test4(void)
{
    char *str = NULL;
    GetMemory4(&str, 100);
    strcpy(str, "hello world");  
    printf(str);    
}


/************************************************************************/
/* 1、运行结果正常打印world,但是操作是非法的                                */  
/* 2、malloc之后需要判断是否成功                                           */
/* 3、strcpy不安全                                                        */
/************************************************************************/
void Test5(void)
{
    char *str = (char *) malloc(100);
    strcpy(str, "hello");
    free(str);    
    //str = NULL 这里应将str置NULL
    if(str != NULL)
    {
        strcpy(str, "world"); //这一步操作非法,free之后str指向的空间不再属于str
        printf(str);
    }
}


/************************************************************************/
/* 1、程序崩溃,str+6之后free,操作了非法地址空间                            */  
/* 2、malloc之后需要判断是否成功                                           */
/* 3、strcpy不安全                                                        */
/************************************************************************/
void Test6()
{
    char *str=(char *)malloc(100);
    strcpy(str, "hello");
    str+=6;
    free(str);
    if(str!=NULL)
    {
        strcpy(str, "world");
        printf(str);
    }
}
int main()
{
    //Test1();
    //Test2();
    //Test3();
    //Test4();
    //Test5();
    Test6();

}

Getmemory问题

原文:https://www.cnblogs.com/CyJack/p/10953272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!