Getmemory问题

摘自: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();

}

posted @ 2019-05-31 09:00  CyJack  阅读(183)  评论(0编辑  收藏  举报