• <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),開心的工作
            簡(jiǎn)單、開放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            c/c++ 柔性數(shù)組

            Posted on 2010-02-23 17:09 S.l.e!ep.¢% 閱讀(627) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++
            c/c++ 柔性數(shù)組
            2009-12-09 09:56
            結(jié)構(gòu)體變長(zhǎng)的妙用——0個(gè)元素的數(shù)組

            有時(shí)我們需要產(chǎn)生一個(gè)結(jié)構(gòu)體,實(shí)現(xiàn)了一種可變長(zhǎng)度的結(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ò)無(wú)法編譯可以改成:)
            typedef?struct?st_type
            {
            ??? int?nCnt;
            ??? int?item[];
            }type_a;
            這樣我們就可以定義一個(gè)可變長(zhǎng)的結(jié)構(gòu),用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那個(gè)0個(gè)元素的數(shù)組沒有占用空間,而后我們可以進(jìn)行變長(zhǎng)操作了。
            C語(yǔ)言版:
            ??? type_a?*p?=?(type_a*)malloc(sizeof(type_a)+100*sizeof(int));
            C++語(yǔ)言版:
            ??? type_a?*p?=?(type_a*)new?char[sizeof(type_a)+100*sizeof(int)];

            這樣我們就產(chǎn)生了一個(gè)長(zhǎng)為100的type_a類型的東西用p->item[n]就能簡(jiǎn)單地訪問可變長(zhǎng)元素,原理十分簡(jiǎn)單,分配了比sizeof(type_a)多的內(nèi)存后int?item[];就有了其意義了,它指向的是int?nCnt;后面的內(nèi)容,是沒有內(nèi)存需要的,而在分配時(shí)多分配的內(nèi)存就可以由其來操控,是個(gè)十分好用的技巧。
            而釋放同樣簡(jiǎn)單:
            C語(yǔ)言版:
            ??? free(p);
            C++語(yǔ)言版:
            ??? 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ì)有警告消息。
            久久天天躁狠狠躁夜夜不卡| 日韩精品久久久久久| 亚洲欧洲中文日韩久久AV乱码| 久久久精品视频免费观看| 手机看片久久高清国产日韩| 久久久久亚洲av无码专区喷水| 国产成人精品久久一区二区三区av| 久久精品综合一区二区三区| 久久精品免费一区二区| 99久久精品九九亚洲精品| 99久久做夜夜爱天天做精品| 久久91精品国产91久久户| 久久久久久精品免费免费自慰| 久久精品aⅴ无码中文字字幕不卡| 日本精品久久久久中文字幕8| 久久人人添人人爽添人人片牛牛| 四虎国产精品免费久久5151| 亚洲中文字幕无码久久精品1| 久久97久久97精品免视看秋霞| 亚洲综合熟女久久久30p| 久久久综合香蕉尹人综合网| 久久精品国产亚洲沈樵| 一本色道久久HEZYO无码| 人妻丰满?V无码久久不卡| 一本色道久久88加勒比—综合| 伊人久久大香线蕉综合Av | 国内精品免费久久影院| 蜜桃麻豆www久久国产精品| 国产精品久久永久免费| 亚洲AV无码久久精品狠狠爱浪潮| 久久精品国产福利国产琪琪| 久久综合狠狠综合久久激情 | 亚洲精品综合久久| 97精品伊人久久久大香线蕉| 国产午夜久久影院| 国产欧美久久久精品| 久久久久久夜精品精品免费啦| 日韩精品久久久久久久电影蜜臀| 久久99久国产麻精品66| 人人狠狠综合久久88成人| 久久天天躁狠狠躁夜夜躁2O2O|