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

            柔性數(shù)組結(jié)構(gòu)成員

            C99中,結(jié)構(gòu)中的最后一個(gè)元素允許是未知大小的數(shù)組,這就叫做柔性數(shù)組成員,但結(jié)構(gòu)中的柔性數(shù)組成員前面必須至少一個(gè)其 他成員。柔性數(shù)組成員允許結(jié)構(gòu)中包含一個(gè)大小可變的數(shù)組。sizeof返回的這種結(jié)構(gòu)大小不包括柔性數(shù)組的內(nèi)存。包含柔性數(shù)組成員的結(jié)構(gòu)用malloc ()函數(shù)進(jìn)行內(nèi)存的動(dòng)態(tài)分配,并且分配的內(nèi)存應(yīng)該大于結(jié)構(gòu)的大小,以適應(yīng)柔性數(shù)組的預(yù)期大小。】 
            C語言大全,“柔性數(shù)組成員”

            【柔性數(shù)組結(jié)構(gòu)成員
              C99中,結(jié)構(gòu)中的最后一個(gè)元素允許是未知大小的數(shù)組,這就叫做柔性數(shù)組成員,但結(jié)構(gòu)中的柔性數(shù)組成員前面必須至少一個(gè)其 他成員。柔性數(shù)組成員允許結(jié)構(gòu)中包含一個(gè)大小可變的數(shù)組。sizeof返回的這種結(jié)構(gòu)大小不包括柔性數(shù)組的內(nèi)存。包含柔性數(shù)組成員的結(jié)構(gòu)用malloc ()函數(shù)進(jìn)行內(nèi)存的動(dòng)態(tài)分配,并且分配的內(nèi)存應(yīng)該大于結(jié)構(gòu)的大小,以適應(yīng)柔性數(shù)組的預(yù)期大小。】 
            C語言大全,“柔性數(shù)組成員”

            看看 C99 標(biāo)準(zhǔn)中 靈活數(shù)組成員:

            結(jié)構(gòu)體變長的妙用——0個(gè)元素的數(shù)組
            有時(shí)我們需要產(chǎn)生一個(gè)結(jié)構(gòu)體,實(shí)現(xiàn)了一種可變長度的結(jié)構(gòu)。如何來實(shí)現(xiàn)呢?
            看這個(gè)結(jié)構(gòu)體的定義:
            typedef struct st_type
            {
            int nCnt;
            int item[0];
            }type_a;
            (有些編譯器會(huì)報(bào)錯(cuò)無法編譯可以改成:)
            typedef struct st_type
            {
            int nCnt;
            int item[];
            }type_a;
            這樣我們就可以定義一個(gè)可變長的結(jié)構(gòu),用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那

            個(gè)0個(gè)元素的數(shù)組沒有占用空間,而后我們可以進(jìn)行變長操作了。
            C語言版:
            type_a *p = (type_a*)malloc(sizeof(type_a)+100*sizeof(int));
            C++語言版:
            type_a *p = (type_a*)new char[sizeof(type_a)+100*sizeof(int)];
            這樣我們就產(chǎn)生了一個(gè)長為100的type_a類型的東西用p->item[n]就能簡單地訪問可變長元素,原理十分簡單

            ,分配了比sizeof(type_a)多的內(nèi)存后int item[];就有了其意義了,它指向的是int nCnt;后面的內(nèi)容,是沒

            有內(nèi)存需要的,而在分配時(shí)多分配的內(nèi)存就可以由其來操控,是個(gè)十分好用的技巧。
            而釋放同樣簡單:
            C語言版:
            free(p);
            C++語言版:
            delete []p;
            其實(shí)這個(gè)叫靈活數(shù)組成員(fleible array member)C89不支持這種東西,C99把它作為一種特例加入了標(biāo)準(zhǔn)。但

            是,C99所支持的是incomplete type,而不是zero array,形同int item[0];這種形式是非法的,C99支持的

            形式是形同int item[];只不過有些編譯器把int item[0];作為非標(biāo)準(zhǔn)擴(kuò)展來支持,而且在C99發(fā)布之前已經(jīng)有

            了這種非標(biāo)準(zhǔn)擴(kuò)展了,C99發(fā)布之后,有些編譯器把兩者合而為一。
            下面是C99中的相關(guān)內(nèi)容:
            6.7.2.1 Structure and union specifiers

            As a special case, the last element of a structure with more than one named member may have 

            an incomplete array type; this is called a flexible array member. With two exceptions, the 

            flexible array member is ignored. First, the size of the structure shall be equal to the offset 

            of the last element of an otherwise identical structure that replaces the flexible array member 

            with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand 

            that is (a pointer to) a structure with a flexible array member and the right operand names that 

            member, it behaves as if that member were replaced with the longest array (with the same element 

            type) that would not make the structure larger than the object being accessed; the offset of the 

            array shall remain that of the flexible array member, even if this would differ from that of the 

            replacement array. If this array would have no elements, it behaves as if it had one element but 

            the behavior is undefined if any attempt is made to access that element or to generate a pointer 

            one past it.
            例如在VC++6里使用兩者之一都能通過編譯并且完成操作,而會(huì)產(chǎn)生warning C4200: nonstandard extension 

            used : zero-sized array in struct/union的警告消息。
            而在DEVCPP里兩者同樣可以使用,并且不會(huì)有警告消息。

            posted on 2016-04-14 11:14 sheng 閱讀(481) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

            <2016年4月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲乱亚洲乱淫久久| 久久AV高清无码| 人妻丰满?V无码久久不卡| 一级做a爰片久久毛片看看| 久久久久久久久波多野高潮| 嫩草伊人久久精品少妇AV| 亚洲国产成人久久综合一 | 亚洲国产精品综合久久网络| 亚洲精品国产综合久久一线| 久久综合88熟人妻| 国产精品日韩欧美久久综合| 久久无码人妻一区二区三区 | 国产亚洲综合久久系列| 久久亚洲欧洲国产综合| 久久香综合精品久久伊人| 日日狠狠久久偷偷色综合免费| 无码人妻精品一区二区三区久久久| 99久久精品免费| 国产精品9999久久久久| 久久狠狠爱亚洲综合影院| 99久久免费只有精品国产| 99久久精品日本一区二区免费| 婷婷国产天堂久久综合五月| 91精品国产综合久久香蕉 | 中文字幕人妻色偷偷久久| 理论片午午伦夜理片久久| 草草久久久无码国产专区| 国产一久久香蕉国产线看观看| 日日噜噜夜夜狠狠久久丁香五月| 亚洲精品美女久久久久99小说| 国产福利电影一区二区三区久久久久成人精品综合 | 久久夜色精品国产噜噜亚洲a| 曰曰摸天天摸人人看久久久| 久久国产亚洲精品无码| 久久婷婷成人综合色综合| 日韩人妻无码一区二区三区久久| 精品久久久久久国产| 久久久久久精品免费免费自慰| 狠狠色综合网站久久久久久久高清 | 久久99精品久久久久久噜噜| 日本福利片国产午夜久久|