zoukankan      html  css  js  c++  java
  • 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();

    }

  • 相关阅读:
    Ubuntu 16.04 samba详细配置及使用(将linux中的共享目录设置成windows中的一个映射磁盘)
    在python上获得随机字符
    Internet Information Services (IIS)管理器 部署网站报错
    SQL Server 2019 安装过程中如何设置二进制
    W: http://archive.ubuntukylin.com:10006/ubuntukylin/dists/xenial/InRelease: Signature by key 6CE35A4EBAB676094476BE7CD259B7555E1D3C58 uses weak digest algorithm (SHA1)
    Django命令行相关命令 以及创建一个空白网页的步骤
    网络爬虫之定向爬虫:爬取当当网2015年图书销售排行榜信息(Crawler)
    Ubuntu 16.04 Django安装和配置
    问题记录2:TypeError: write() argument must be str, not bytes
    问题记录1:The VMware Authorization Service is not running.
  • 原文地址:https://www.cnblogs.com/CyJack/p/10953272.html
Copyright © 2011-2022 走看看