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

            柔性數組結構成員

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

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

            看看 C99 標準中 靈活數組成員:

            結構體變長的妙用——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里兩者同樣可以使用,并且不會有警告消息。

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

            導航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            激情五月综合综合久久69| 性欧美丰满熟妇XXXX性久久久| 久久青草国产手机看片福利盒子| 久久精品国产亚洲AV无码娇色| 99久久99久久精品国产片果冻| 久久综合狠狠综合久久97色| 欧美午夜精品久久久久免费视| 伊人久久大香线蕉精品| 久久午夜夜伦鲁鲁片免费无码影视| 久久99精品国产自在现线小黄鸭 | 少妇精品久久久一区二区三区 | 亚洲狠狠久久综合一区77777| 久久人人爽人人爽人人片AV麻豆| 欧洲人妻丰满av无码久久不卡| 久久婷婷人人澡人人| 精品无码久久久久久午夜| 婷婷国产天堂久久综合五月| 高清免费久久午夜精品| 狠狠色丁香久久婷婷综合_中| 一级做a爰片久久毛片人呢| 99久久99这里只有免费费精品 | 久久综合一区二区无码| 99热成人精品热久久669| 亚洲日韩中文无码久久| 久久亚洲视频| 久久亚洲AV无码西西人体| 久久香蕉一级毛片| 久久久久久久久久久久中文字幕 | 91性高湖久久久久| 国产亚洲色婷婷久久99精品| 热re99久久精品国99热| 久久精品国产日本波多野结衣| 久久国产免费直播| 国内精品久久久久久久久| 99久久99久久精品国产片| 99久久精品这里只有精品| 2020最新久久久视精品爱| 伊人久久免费视频| 久久精品国产72国产精福利| 国产午夜电影久久| 久久综合给合综合久久|