• <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>
            posts - 297,  comments - 15,  trackbacks - 0
            首先看個問題程序:
            #include <stdlib.h>
            #include <stdio.h>
            void main()
            {
            int *i;
            i=(int *)malloc(sizeof(int));
            *i=1;
            *(i+1)=2;
            printf(\"%x|%d\\n\",i,*i);
            printf(\"%x|%d\",i+1,*(i+1));
            }
            輸出的結(jié)果是:
            8fc|1
            8fe|2
            這個程序編譯通過,運行正常,說它有問題,問題出在哪呢?

            首先通過malloc,建了一個大小為2的堆,
            i指向的地址是8fc,i+1指向的地址是8fc+sizeof(int)=8fe
            但是地址8fe是不受保護的,因為它不是機器分配給i+1的,隨時會被其他變量占用。

            正確的做法是
            #include \"stdlib.h\"
            #include \"stdio.h\"
            void main()
            {
            int *i;
            i=(int *)malloc(sizeof(int));
            *i=1;
            i=(int *)realloc(i,2*sizeof(int));
            *(i+1)=2;
            printf(\"%x|%d\\n\",i,*i);
            printf(\"%x|%d\",i+1,*(i+1));
            }
            realloc 可以對給定的指針所指的空間進行擴大或者縮小,無論是擴張或是縮小,原有內(nèi)存的中內(nèi)容將保持不變。當然,對于縮小,則被縮小的那一部分的內(nèi)容會丟失。 realloc 并不保證調(diào)整后的內(nèi)存空間和原來的內(nèi)存空間保持同一內(nèi)存地址。相反,realloc 返回的指針很可能指向一個新的地址。
            所以,在代碼中,我們必須將realloc返回的值,重新賦值給 p :
            p = (int *) realloc (p, sizeof(int) *15);

            甚至,你可以傳一個空指針(0)給 realloc ,則此時realloc 作用完全相當于malloc。
            int* p = (int *) realloc (0,sizeof(int) * 10); //分配一個全新的內(nèi)存空間,

            這一行,作用完全等同于:
            int* p = (int *) malloc(sizeof(int) * 10);

            from:
            http://blog.c114.net/html/07/206807-17133.html

            posted on 2010-03-01 22:07 chatler 閱讀(223) 評論(0)  編輯 收藏 引用 所屬分類: C++_BASIS
            <2011年6月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關(guān),覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            国産精品久久久久久久| 久久久久亚洲AV成人网人人网站| 一级女性全黄久久生活片免费 | 99精品国产99久久久久久97| 久久精品?ⅴ无码中文字幕| 久久精品一区二区| 91精品国产91久久久久久青草| 九九久久99综合一区二区| AV无码久久久久不卡蜜桃 | 一本色道久久88加勒比—综合| 久久久久人妻一区二区三区vr| 一本色道久久88—综合亚洲精品| 久久精品国产亚洲AV不卡| 狠狠色丁香婷婷久久综合| 久久久久人妻一区二区三区| 日本五月天婷久久网站| 99精品国产综合久久久久五月天| 亚洲∧v久久久无码精品| 97精品国产91久久久久久| 欧美伊香蕉久久综合类网站| 国产综合免费精品久久久| 色99久久久久高潮综合影院| 久久99热这里只有精品66| 蜜臀久久99精品久久久久久小说| 97精品伊人久久大香线蕉app | 爱做久久久久久| 久久久久亚洲av综合波多野结衣| 996久久国产精品线观看| 久久无码国产| 国内精品久久久人妻中文字幕| 久久综合九色综合久99| 伊人色综合久久天天网| 99久久国产热无码精品免费| 久久久久久久久久免免费精品| 亚洲午夜久久久久妓女影院| 久久电影网一区| 久久精品免费一区二区| 国产日韩久久久精品影院首页| 亚洲精品无码成人片久久| 久久久精品国产Sm最大网站| 久久精品中文字幕无码绿巨人|