• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Welcome to ErranLi's Blog!

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(12)

            搜索

            •  

            積分與排名

            • 積分 - 175094
            • 排名 - 150

            最新評論

            閱讀排行榜

            開發(fā)中發(fā)現(xiàn)在進行 賦值操作的時候,很容易內存丟失,看實例:

            class CEntity
            {
            public:
                CEntity(char flag);
                ~CEntity();
            private: 
                char m_flag;
            };

            CEntity::CEntity(char flag)
            {
                m_flag = flag; 
                cout<<"constructing entity "<<m_flag<<endl; 
            }

            CEntity::~CEntity()
            {
             cout<<"destructing entity "<<m_flag<<endl;
            }

            執(zhí)行代碼:
            int main(int argc, char* argv[])
            {
                CEntity m('m'),n('n');
                m = n;
                return 0;
            }

            輸出為:
            constructing entity m
            constructing entity n
            destructing entity n
            destructing entity n

            這個沒有出現(xiàn)任何問題,很成功,因為在CEntity里面沒有new任何東西,如果把CEntity該為:
            class CEntity
            {
            enum {SIZE = 10};
            public:
                CEntity(char flag);
                ~CEntity();
            private:     
                char * m_flag;
            };

            CEntity::CEntity(char flag)
            {
                m_flag = new char[SIZE ];
                m_flag[0] = flag; 
                cout<<"constructing entity "<<m_flag[0]<<endl; 
            }

            CEntity::~CEntity()
            {
             cout<<"destructing entity "<<m_flag[0]<<endl;
            delete[] m_flag;
            }

            執(zhí)行同樣的main函數(shù),程序將無法運行,debug assertion error!

            這個問題該怎么解決?
            先睡覺


            如oosky所說,重載了賦值操作符,問題就解決了,


            CEntity & CEntity::operator=(const CEntity & ent)
            {
                if (ent.m_flag == NULL)
                {
                    if (m_flag != NULL)
                    {
                        delete [] m_flag;
                        m_flag = NULL;
                    }
                }
                else 
                {
                    if (m_flag == NULL)
                    {
                        m_flag = new char[SIZE];
                    }
              
                    for(int i=0; i<SIZE; i++)
                    {  
                           m_flag[i] = ent.m_flag[i];
                    }
                }
                return *this;
            }

            由此可見自定義對象之間的賦值操作要小心行事.....

             

            posted on 2006-05-21 00:47 erran 閱讀(798) 評論(5)  編輯 收藏 引用 所屬分類: C & C++

            Feedback

            # re: C++隨筆 關于賦值操作 內存丟失 2006-05-21 14:27 oosky
            m = n;
            操作執(zhí)行了默認的賦值函數(shù),兩個對象都指向同一塊地址,然后析構的時候對同一地址delete兩次,并且在賦值的時候丟失了一塊內存,造成了內存泄露。
            所以自己寫賦值函數(shù),才能解決問題。  回復  更多評論
              

            # re: C++隨筆 關于賦值操作 內存丟失 2006-05-22 16:38 任我行
            CEntity & CEntity::operator=(const CEntity & ent)
            你的這個函數(shù)還是有問題。
            else
            {
            if (m_flag == NULL)
            {
            m_flag = new char[SIZE];
            }
            在這里。
            m_flag!=NULL的時候直接m_flag = NULL就造成了內存泄漏,原來的m_flag指向的內存丟失。
            if(m_flag != NULL) delete []m_flag;
            m_flag = new char[size];

              回復  更多評論
              

            # re: C++隨筆 關于賦值操作 內存丟失 2006-05-22 21:10 erran
            沒看出來,好想沒問題吧,是不是看錯了  回復  更多評論
              

            # re: C++隨筆 關于賦值操作 內存丟失 2006-05-23 08:53 任我行
            嗯,是看錯了.
            CEntity & CEntity::operator=(const CEntity & ent)
            {
            if(m_flag != NULL){
            delete []m_flag;
            m_flag = NULL;
            }
            if(ent.m_flag!=NULL)
            memcpy(m_flag, ent.m_flag, SIZE);
            return *this;
            }  回復  更多評論
              

            # re: C++隨筆 關于賦值操作 內存丟失[未登錄] 2008-08-28 12:56 Henry
            可以定義一個拷貝構造函數(shù)試試  回復  更多評論
              

            青青草原综合久久大伊人导航| 久久久久久久波多野结衣高潮 | 亚洲性久久久影院| 日本精品久久久久影院日本 | 一本久久a久久精品综合香蕉| 亚洲а∨天堂久久精品9966| 麻豆成人久久精品二区三区免费| 久久国产成人精品麻豆| 伊人久久大香线蕉综合网站| 精品久久久久久成人AV| 久久男人中文字幕资源站| 久久精品毛片免费观看| 久久午夜综合久久| 国产亚洲欧美成人久久片| 久久久久国产精品嫩草影院| 爱做久久久久久| 狠狠色婷婷久久一区二区三区| 亚洲国产天堂久久久久久| 久久精品9988| 国产精品99久久精品| 伊人久久大香线蕉亚洲五月天 | 狠狠色丁香久久综合婷婷| 久久久噜噜噜久久中文字幕色伊伊| 亚洲午夜久久影院| 久久国产精品无码一区二区三区 | 91精品国产综合久久久久久| 精品久久久久久无码不卡| 精品久久久久久无码中文野结衣 | 99精品国产在热久久无毒不卡| 老男人久久青草av高清| 婷婷久久综合九色综合绿巨人| 久久99精品久久久久久齐齐 | 久久精品人人做人人爽电影蜜月| 伊人色综合九久久天天蜜桃| 免费一级欧美大片久久网| 狠狠人妻久久久久久综合| 精品免费久久久久国产一区| 久久国产影院| 一本大道久久东京热无码AV| 日本WV一本一道久久香蕉| 久久天天躁狠狠躁夜夜不卡|