• <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.¢%

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

            c/c++ 柔性數組

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

            有時我們需要產生一個結構體,實現了一種可變長度的結構。如何來實現呢?
            看這個結構體的定義:
            typedef?struct?st_type
            {
            ??? int?nCnt;
            ??? int?item[0];
            }type_a;
            (有些編譯器會報錯無法編譯可以改成:)
            typedef?struct?st_type
            {
            ??? int?nCnt;
            ??? int?item[];
            }type_a;
            這樣我們就可以定義一個可變長的結構,用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那個0個元素的數組沒有占用空間,而后我們可以進行變長操作了。
            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)];

            這樣我們就產生了一個長為100的type_a類型的東西用p->item[n]就能簡單地訪問可變長元素,原理十分簡單,分配了比sizeof(type_a)多的內存后int?item[];就有了其意義了,它指向的是int?nCnt;后面的內容,是沒有內存需要的,而在分配時多分配的內存就可以由其來操控,是個十分好用的技巧。
            而釋放同樣簡單:
            C語言版:
            ??? free(p);
            C++語言版:
            ??? delete?[]p;

            其實這個叫靈活數組成員(fleible?array?member)C89不支持這種東西,C99把它作為一種特例加入了標準。但是,C99所支持的是incomplete?type,而不是zero?array,形同int?item[0];這種形式是非法的,C99支持的形式是形同int?item[];只不過有些編譯器把int?item[0];作為非標準擴展來支持,而且在C99發布之前已經有了這種非標準擴展了,C99發布之后,有些編譯器把兩者合而為一。

            下面是C99中的相關內容:
            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里使用兩者之一都能通過編譯并且完成操作,而會產生warning C4200: nonstandard extension

            used : zero-sized array in struct/union的警告消息。
            而在DEVCPP里兩者同樣可以使用,并且不會有警告消息。
            久久无码国产专区精品| 狠狠色丁香婷综合久久| 久久人人爽人爽人人爽av| 久久这里有精品视频| 漂亮人妻被中出中文字幕久久| 久久亚洲日韩精品一区二区三区| 久久久久亚洲AV无码麻豆| 91精品无码久久久久久五月天| 午夜精品久久久久成人| 久久久久无码精品国产不卡| 久久国产精品免费一区| 丁香色欲久久久久久综合网| 夜夜亚洲天天久久| 色天使久久综合网天天| 国产99久久久国产精免费| 亚洲精品国产字幕久久不卡| 久久国产热这里只有精品| 国产成人久久AV免费| 久久婷婷五月综合国产尤物app| 91久久精品电影| 91精品国产高清91久久久久久| 中文字幕精品无码久久久久久3D日动漫 | 久久91这里精品国产2020| 久久久久国产精品嫩草影院| 久久精品国产久精国产| 久久99精品免费一区二区| 无码8090精品久久一区| 国产69精品久久久久APP下载 | 一级女性全黄久久生活片免费 | 色欲综合久久躁天天躁| 97久久精品午夜一区二区| 97久久精品人妻人人搡人人玩| 久久综合国产乱子伦精品免费| 18岁日韩内射颜射午夜久久成人| 香蕉久久久久久狠狠色| 久久精品国产亚洲AV蜜臀色欲| 久久影视国产亚洲| 久久婷婷激情综合色综合俺也去| 久久99精品国产自在现线小黄鸭| 久久无码AV一区二区三区| 久久青青草原亚洲av无码app|