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

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開心的工作
            簡單、開放、平等的公司文化;尊重個性、自由與個人價值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            realloc 函數(shù)的使用

            Posted on 2009-12-18 16:54 S.l.e!ep.¢% 閱讀(1486) 評論(0)  編輯 收藏 引用 所屬分類: C++
            1. realloc失敗的時候,返回NULL

            2. realloc失敗的時候,原來的內(nèi)存不改變,也就是不free或不move,(這個地方很容易出錯)

            3. 假如原來的內(nèi)存后面還有足夠多剩余內(nèi)存的話,realloc的內(nèi)存=原來的內(nèi)存+剩余內(nèi)存,realloc還是返回原來內(nèi)存的地址; 假如原來的內(nèi)存后面沒有足夠多剩余內(nèi)存的話,realloc將申請新的內(nèi)存,然后把原來的內(nèi)存數(shù)據(jù)拷貝到新內(nèi)存里,原來的內(nèi)存將被free掉,realloc返回新內(nèi)存的地址

            4. 如果size為0,效果等同于free()

            5. 傳遞給realloc的指針必須是先前通過malloc(), calloc(), 或realloc()分配的

            6. 傳遞給realloc的指針可以為空,等同于malloc。

            /*?realloc.c?-?C?standard?library?routine.
            ???Copyright?(c)?1989,?1993??Michael?J.?Haertel
            ???You?may?redistribute?this?library?under?the?terms?of?the
            ???GNU?Library?General?Public?License?(version?2?or?any?later
            ???version)?as?published?by?the?Free?Software?Foundation.
            ???THIS?SOFTWARE?IS?PROVIDED?"AS?IS"?WITHOUT?ANY?EXPRESS?OR?IMPLIED
            ???WARRANTY.??IN?PARTICULAR,?THE?AUTHOR?MAKES?NO?REPRESENTATION?OR
            ???WARRANTY?OF?ANY?KIND?CONCERNING?THE?MERCHANTABILITY?OF?THIS
            ???SOFTWARE?OR?ITS?FITNESS?FOR?ANY?PARTICULAR?PURPOSE.?
            */

            #include?
            <limits.h>
            #include?
            <stddef.h>
            #include?
            <stdlib.h>
            #include?
            <string.h>
            #include?
            "malloc.h"

            #ifdef?__ELF__
            #pragma?weak?realloc?
            =?__libc_realloc
            #endif

            #define?MIN(A,?B)?((A)?<?(B)???(A)?:?(B))

            /*?Resize?the?given?region?to?the?new?size,?returning?a?pointer
            ???to?the?(possibly?moved)?region.??This?is?optimized?for?speed;
            ???some?benchmarks?seem?to?indicate?that?greater?compactness?is
            ???achieved?by?unconditionally?allocating?and?copying?to?a
            ???new?region.?
            */
            void?*
            __libc_realloc?(
            void?*ptr,?size_t?size)
            {
            ????
            void?*result,?*previous;
            ????
            int?block,?blocks,?type;
            ????
            int?oldlimit;

            ????
            if?(!ptr)
            ????????
            return?__libc_malloc(size);
            ????
            if?(!size)?{
            ????????__libc_free(ptr);
            ????????
            return?__libc_malloc(0);
            ????}

            ????block?
            =?BLOCK(ptr);

            ????
            switch?(type?=?_heapinfo[block].busy.type)?{
            ????
            case?0:
            ????????
            /*?Maybe?reallocate?a?large?block?to?a?small?fragment.?*/
            ????????
            if?(size?<=?BLOCKSIZE?/?2)?{
            ????????????
            if?((result?=?__libc_malloc(size))?!=?NULL)?{
            ????????????????????memcpy(result,?ptr,?size);
            #if?1
            ????????????????????__libc_free(ptr);
            #else
            ????????????????????_free_internal(ptr);
            #endif

            ????????????}
            ????????????
            return?result;
            ????????}

            ????????
            /*?The?new?size?is?a?large?allocation?as?well;?see?if
            ???????????we?can?hold?it?in?place.?
            */
            ????????blocks?
            =?BLOCKIFY(size);
            ????????
            if?(blocks?<?_heapinfo[block].busy.info.size)?{
            ????????????
            /*?The?new?size?is?smaller;?return?excess?memory
            ???????????????to?the?free?list.?
            */
            ????????????_heapinfo[block?
            +?blocks].busy.type?=?0;
            ????????????_heapinfo[block?
            +?blocks].busy.info.size
            ????????????????
            =?_heapinfo[block].busy.info.size?-?blocks;
            ????????????_heapinfo[block].busy.info.size?
            =?blocks;
            #if?1
            ????????????__libc_free(ADDRESS(block?
            +?blocks));
            #else
            ????????????_free_internal(ADDRESS(block?
            +?blocks));
            #endif
            ????????????
            return?ptr;
            ????????}?
            else?if?(blocks?==?_heapinfo[block].busy.info.size)
            ????????????
            /*?No?size?change?necessary.?*/
            ????????????
            return?ptr;
            ????????
            else?{
            ????????????
            /*?Won't?fit,?so?allocate?a?new?region?that?will.??Free
            ???????????????the?old?region?first?in?case?there?is?sufficient?adjacent
            ???????????????free?space?to?grow?without?moving.?
            */
            ????????????blocks?
            =?_heapinfo[block].busy.info.size;
            ????????????
            /*?Prevent?free?from?actually?returning?memory?to?the?system.?*/
            ????????????oldlimit?
            =?_heaplimit;
            ????????????_heaplimit?
            =?0;
            #if?1
            ????????????__libc_free(ptr);
            #else
            ????????????_free_internal(ptr);
            #endif
            ????????????_heaplimit?
            =?oldlimit;
            ????????????result?
            =?__libc_malloc(size);
            ????????????
            if?(!result)?{
            ????????????????
            /*?Now?we're?really?in?trouble.??We?have?to?unfree
            ???????????????????the?thing?we?just?freed.??Unfortunately?it?might
            ???????????????????have?been?coalesced?with?its?neighbors.?
            */
            ????????????????
            if?(_heapindex?==?block)
            ????????????????????__libc_malloc(blocks?
            *?BLOCKSIZE);
            ????????????????
            else?{
            ????????????????????previous?
            =?malloc((block?-?_heapindex)?*?BLOCKSIZE);
            ????????????????????__libc_malloc(blocks?
            *?BLOCKSIZE);
            #if?1
            ????????????????????__libc_free(previous);
            #else
            ????????????????????_free_internal(previous);
            #endif
            ????????????????}????????????
            ????????????????
            return?NULL;
            ????????????}
            ????????????
            if?(ptr?!=?result)
            ????????????????memmove(result,?ptr,?blocks?
            *?BLOCKSIZE);
            ????????????
            return?result;
            ????????}
            ????????
            break;

            ????
            default:
            ????????
            /*?Old?size?is?a?fragment;?type?is?logarithm?to?base?two?of
            ???????????the?fragment?size.?
            */
            ????????
            if?((size?>?1?<<?(type?-?1))?&&?(size?<=?1?<<?type))
            ????????????
            /*?New?size?is?the?same?kind?of?fragment.?*/
            ????????????
            return?ptr;
            ????????
            else?{
            ????????????
            /*?New?size?is?different;?allocate?a?new?space,?and?copy
            ???????????????the?lesser?of?the?new?size?and?the?old.?
            */
            ????????????result?
            =?__libc_malloc(size);
            ????????????
            if?(!result)
            ????????????????
            return?NULL;
            ????????????memcpy(result,?ptr,?MIN(size,?
            1?<<?type));
            ????????????__libc_free(ptr);
            ????????????
            return?result;
            ????????}
            ????????
            break;
            ????}
            }

            </code>

            以上就是?libc??庫中?recalloc()?函數(shù)的實現(xiàn),發(fā)現(xiàn)在新的內(nèi)存申請成功的時候,程序是在做了?p
            =malloc(size);?
            ????memcpy(p,?ptr,?size);
            ????free(ptr);
            三步操作的。
            人妻无码精品久久亚瑟影视| 久久久久久精品久久久久| 囯产精品久久久久久久久蜜桃| 国产91久久综合| 国产精品久久久久久久久鸭| 亚洲综合精品香蕉久久网| 无码乱码观看精品久久| 久久99热这里只有精品国产| 热久久这里只有精品| 热99re久久国超精品首页| 久久免费国产精品一区二区| 97久久天天综合色天天综合色hd | 超级97碰碰碰碰久久久久最新| 国产精品va久久久久久久| 久久99精品国产麻豆宅宅| 99久久免费国产精品热| 国产精品18久久久久久vr | 亚洲色婷婷综合久久| 国产精品美女久久福利网站| 国内精品伊人久久久久妇| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久久久久人妻无码| 国产精品美女久久久m| 久久99免费视频| 久久国产成人午夜AV影院| 伊人热热久久原色播放www| 伊人久久大香线蕉亚洲五月天| 久久亚洲AV成人出白浆无码国产| 精品国际久久久久999波多野| 久久无码av三级| 午夜福利91久久福利| 亚洲精品美女久久777777| www.久久精品| 久久九九免费高清视频| 狠狠色丁香久久婷婷综合图片 | 日韩AV无码久久一区二区| 九九久久自然熟的香蕉图片| 99久久国产主播综合精品| 中文字幕无码久久久| 99久久精品国产高清一区二区 | 久久久国产乱子伦精品作者|