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

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            下標是空的數組

            最近看到這種代碼:
            struct mytype
            {
            int cnt;
            int data[0];//
            這個,是什么啊!0個元素的數組????編譯器居然通過了!!!什么東西啊??
            };
            ---------------------------------------------------------------

            int data[0];//
            定義一個數組0個元素,編譯沒有錯,但是這個不能用于輸入,因為是沒有元素的數組
            ---------------------------------------------------------------

            所以當輸入的時候,就等于越界了。
            ---------------------------------------------------------------

            樓主可以算算一下數組的大小:

            sizeof(mytype.data);//
            這樣可能有的編譯器會報錯,有的可能等于0
            ---------------------------------------------------------------

            這是一種trick,用來做變長數組
            ---------------------------------------------------------------

            這個叫做靈活數組成員

            看我的文章

            結構體變長的妙用——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)];
            這樣我們就產生了一個長為100type_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里兩者同樣可以使用,并且不會有警告消息。

            ---------------------------------------------------------------

            data
            是用來做變長數組的,
            不過一般都是 長度為 ...
            ---------------------------------------------------------------

            一般這樣用

            先定義一個指向該struct的指針p
            然后
            p=malloc(sizeof( mytype)+ user_length);
            p->cnt = user_length;

            這樣就相當于有了一個可變長的數組,其長度保存在p->cnt當中。


            ---------------------------------------------------------------

            變長數組,不過這種實現變長數組的方法比較晦澀
            ---------------------------------------------------------------

            呵呵.你要學的東西多了..
            看看linux 源碼,特別tcp/ip,網絡相關的.很多這樣的應用..
            ---------------------------------------------------------------

            變長數組!!
            沒有什么特別的,和平常的內存分配一個樣,
            我們以前使用
            Type* t = (Type*)malloc(sizeof(Type));
            這個只是分配剛好存放Type的地方,其實我們也可以分配比Type類型少的內存,
            比如:
            typedef struct st_type
            {
            int nCnt;
            int item[4];
            }type_a;
            --------------------
            type_a * a=(type_a*)malloc(4);//
            剛好分配了type_a::nCnt的空間而已!
            那也肯定可以操作,只是item的空間沒有存在而已,比如:
            a->nCnt = 2;
            cout<<a->nCnt<<endl;

            ------------------------------------------
            當然也可以分配多啊,剩下的東西怎么處理看程序員的操作啊!
            其實
            typedef struct st_type
            {
            int nCnt;
            int item[0];
            }type_a;
            type_a* a = (type_a*)malloc(104);
            的意思中item只是給編譯器一個地址的起始,多了這個內存就是多余了拉!但多余的內存和我們

            posted on 2010-04-01 16:42 肥仔 閱讀(967) 評論(0)  編輯 收藏 引用 所屬分類: C++ 基礎

            亚洲精品乱码久久久久久自慰| 久久久久久久尹人综合网亚洲 | 国产成人久久久精品二区三区| 国产精品99久久精品| 欧美伊香蕉久久综合类网站| 四虎影视久久久免费观看| 欧洲成人午夜精品无码区久久| 99久久婷婷免费国产综合精品| 久久久久久国产精品无码下载 | 国产精品久久久久…| 国产亚州精品女人久久久久久 | 浪潮AV色综合久久天堂| 91亚洲国产成人久久精品| 亚洲AV无码久久精品蜜桃| 久久AAAA片一区二区| 成人妇女免费播放久久久| 超级碰碰碰碰97久久久久| Xx性欧美肥妇精品久久久久久 | 一本一本久久A久久综合精品 | 久久影院久久香蕉国产线看观看| 久久99精品久久久久久久不卡| 亚洲综合久久夜AV | 狠狠人妻久久久久久综合蜜桃| 久久久精品国产sm调教网站| 久久这里都是精品| 久久精品国产福利国产琪琪| a级成人毛片久久| 日韩乱码人妻无码中文字幕久久| 要久久爱在线免费观看| 久久这里只有精品视频99| 精品国产综合区久久久久久 | 久久婷婷五月综合成人D啪| 久久精品夜色噜噜亚洲A∨| 国产农村妇女毛片精品久久| 久久综合九色综合久99| 久久香蕉一级毛片| 99久久精品免费国产大片| 99久久免费只有精品国产| 曰曰摸天天摸人人看久久久| 99久久精品免费国产大片| 久久九九久精品国产|