• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(12)

            搜索

            •  

            積分與排名

            • 積分 - 175922
            • 排名 - 151

            最新評論

            閱讀排行榜

            開發中發現在進行 賦值操作的時候,很容易內存丟失,看實例:

            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;
            }

            執行代碼:
            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

            這個沒有出現任何問題,很成功,因為在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;
            }

            執行同樣的main函數,程序將無法運行,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 閱讀(807) 評論(5)  編輯 收藏 引用 所屬分類: C & C++

            Feedback

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

            # re: C++隨筆 關于賦值操作 內存丟失 2006-05-22 16:38 任我行
            CEntity & CEntity::operator=(const CEntity & ent)
            你的這個函數還是有問題。
            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
            可以定義一個拷貝構造函數試試  回復  更多評論
              

            亚洲人成无码www久久久| 国产精品久久久久久| 久久被窝电影亚洲爽爽爽| 精品久久久久久国产| 久久久久久久综合狠狠综合| 国产精品伊人久久伊人电影| 一级做a爱片久久毛片| 99久久精品免费国产大片| 91精品国产综合久久四虎久久无码一级| 精品国产VA久久久久久久冰| 久久久久无码精品国产不卡| 久久精品www人人爽人人| 日韩久久久久久中文人妻| 亚洲va中文字幕无码久久| 色婷婷综合久久久久中文| 国产婷婷成人久久Av免费高清| 久久亚洲精品国产精品| 91精品国产高清91久久久久久 | 亚洲国产精品婷婷久久| 欧美久久精品一级c片片| 久久91精品综合国产首页| 久久一本综合| 老色鬼久久亚洲AV综合| 一级做a爰片久久毛片16| 久久久久久久91精品免费观看| 亚洲精品乱码久久久久久蜜桃不卡| 亚洲欧美日韩中文久久| 久久er国产精品免费观看2| 国产视频久久| 中文字幕热久久久久久久| 99麻豆久久久国产精品免费| 久久久久亚洲精品中文字幕| 久久综合久久美利坚合众国| 久久99国产精品二区不卡| 思思久久99热免费精品6| 精品久久无码中文字幕| 亚洲欧美另类日本久久国产真实乱对白| 97久久国产综合精品女不卡| 岛国搬运www久久| 国产精品女同久久久久电影院| 久久AAAA片一区二区|