• <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)

            搜索

            •  

            積分與排名

            • 積分 - 175932
            • 排名 - 151

            最新評論

            閱讀排行榜

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

            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

            這個(gè)沒有出現(xiàn)任何問題,很成功,因?yàn)樵贑Entity里面沒有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ù),程序?qū)o法運(yùn)行,debug assertion error!

            這個(gè)問題該怎么解決?
            先睡覺


            如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++隨筆 關(guān)于賦值操作 內(nèi)存丟失 2006-05-21 14:27 oosky
            m = n;
            操作執(zhí)行了默認(rèn)的賦值函數(shù),兩個(gè)對象都指向同一塊地址,然后析構(gòu)的時(shí)候?qū)ν坏刂穌elete兩次,并且在賦值的時(shí)候丟失了一塊內(nèi)存,造成了內(nèi)存泄露。
            所以自己寫賦值函數(shù),才能解決問題。  回復(fù)  更多評論
              

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

              回復(fù)  更多評論
              

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

            # re: C++隨筆 關(guān)于賦值操作 內(nèi)存丟失 2006-05-23 08:53 任我行
            嗯,是看錯(cuò)了.
            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;
            }  回復(fù)  更多評論
              

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

            精品无码久久久久国产动漫3d| 国产激情久久久久影院小草 | 亚洲精品NV久久久久久久久久 | 久久久亚洲欧洲日产国码aⅴ| 无码久久精品国产亚洲Av影片| 一本久久a久久精品vr综合| 狠狠色丁香久久综合婷婷| 国产三级观看久久| 久久久久久久91精品免费观看| 久久99久久99精品免视看动漫| 精品免费久久久久国产一区| 久久久久人妻一区精品性色av | 99久久99久久精品国产| 日韩精品久久无码人妻中文字幕 | 思思久久99热只有频精品66| 亚洲精品无码久久一线| 国产免费久久精品丫丫| 亚洲精品乱码久久久久久| 国内精品久久久久久久久电影网| 99久久国产精品免费一区二区 | 伊人久久大香线蕉综合Av| 国产精品日韩欧美久久综合| 欧美丰满熟妇BBB久久久| 欧美成人免费观看久久| 91久久精品电影| 国产精品免费看久久久| 九九精品久久久久久噜噜| 午夜精品久久久久久影视777| 亚洲一区二区三区日本久久九| 久久久久亚洲av成人网人人软件| 国产精品热久久无码av| 97热久久免费频精品99| 久久精品免费观看| 久久精品国产亚洲AV无码娇色| 久久香综合精品久久伊人| 久久这里都是精品| 国产精品成人久久久| 久久经典免费视频| 久久人人爽人人爽人人av东京热| 欧美性猛交xxxx免费看久久久| 久久精品国产WWW456C0M|