• new delete


    /*new delete*/

    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream.h>
    #include <string.h>

    struct student
    {
        student()
        {
            cout << "student::student()" << endl;
        }
        student( char* pStrName )
        {
            cout << "student::student(char*)" << pStrName <<  endl;       
            strcpy(szName,pStrName);
        }
        ~student()
        {
            cout << this << " : student::~student()" << endl;
        }
        void KillMySelf()
        {
            //
    这样就析构自己了
            delete this;
        }
        char szName[32];
    };

    void FunCopyData( char* pChar )

        if ( pChar )
        {
            strcpy( pChar,"Hello");
        }
    }

    void FunDelPtrRef( char*& pStr )
    {
        if ( pStr )
        {
            delete[] pStr;
            pStr = NULL;
        }
    }

    void FunDelPtrBy2( char** ppStr )
    {
        if ( *ppStr )
        {
            delete[] *ppStr;
            *ppStr = NULL;
        }
    }

    void FunNew()
    {
        char *pChar = new char[10];
       
        strcpy( pChar,"New Space");   
        FunCopyData( pChar );   
        //
    指针做参数,进行释放,形参设置为0,和实参数没有改变
        //
    解决方法 1: 二级别指针解决  2:指针的引用
        FunDelPtrBy2( &pChar );
        FunDelPtrRef( pChar );
    }

    int main(int argc, char* argv[])
    {
        FunNew();
       
        student *p = new student("
    张三");
        //
    对象数组
        student ObjArray[3] = { "
    张三","李四","王五" };   
        //
    无参的构造函数构造三个对象
        student *pHeapArray = new student[3];
        //
    对象指针数组,与上面个有区别
        student *ObjPtrArray[3] = { new student("
    张三"),
                                    new student("
    李四"),
                                    new student("
    王五") };
       
        //malloc
    不带数据类型,不会有构造函数调用
        //void *p = malloc(sizeof(student));
       
        //
    不要破坏堆内存的结构,就是不要越界访问
        //p->szName[32] = 'A';
       
        //[]
    成对使用,如果new时有[],那么在delete时也要带上[], new/delete成对使用
        if ( p )
        {
            delete p;
            p = NULL;
        }
       
        for ( int i = 0 ; i < 3 ; i++ )
        {
            if ( ObjPtrArray[i] )
            {
                delete ObjPtrArray[i];
               
                ObjPtrArray[i] = NULL;
            }
        }
       
        if ( pHeapArray )
        {
            delete[] pHeapArray;
            pHeapArray = NULL;
        }
       
        //delete this;
    在成员函数中,做2个步骤
        //1.
    调用析构 2.释放堆空间
        student * pNewObj = new student;
        //
    这样堆空间就没产生
        pNewObj->KillMySelf();
        pNewObj = NULL;
       
        //
    在使用delete this的时候,一定要保证后面没有再使用的到这个对象
       
        return 0;
    }

  • 相关阅读:
    [设计模式]在CodeDom代码生成中使用Decorator模式实现类型创建
    【翻译】防腐层:面向领域驱动设计的更为稳健的系统集成方案
    EntityFramework之领域驱动设计实践【后续篇】:基于EF 4.3.1 Code First的领域驱动设计实践案例
    Apworks框架中各种仓储实现的性能基准测试与结果对比
    CQRS架构中同步服务的一种实现方式
    在Visual Studio 2010中创建多项目(解决方案)模板【三】
    Microsoft NLayerApp案例理论与实践 应用层
    在Visual Studio 2010中创建多项目(解决方案)模板【二】
    小猫奥斯卡
    测试一下又拍网图片外链
  • 原文地址:https://www.cnblogs.com/w413133157/p/1653632.html
Copyright © 2020-2023  润新知