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

            小明思考

            高性能服務(wù)器端計算
            posts - 70, comments - 428, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            奇怪的g++的行為

            Posted on 2008-08-13 16:56 小明 閱讀(3630) 評論(18)  編輯 收藏 引用 所屬分類: C/C++
            下面的C++代碼能編譯么?

            #include <stdio.h>
            #define NUM  getnum()

            int getnum()
            {
                
            int x = 0;
                scanf(
            "%d"&x);
                printf(
            "%d\n", x);
                
            return x;
            }

            int main()
            {
                    
            int array[NUM];
                    printf(
            "array size =%d\n",sizeof(array));
                    
            return 0;
            }

            在g++中居然可以編譯。
            輸入10,返回array size=40
            輸入20,返回array size=80
            輸入-1,返回array size =-4!!

            問題:
            1.這樣的做法符合C++標(biāo)準(zhǔn)么?連sizeof成了運行期計算
            2.這個array的空間應(yīng)該分配在heap上,而不是stack上。g++做了什么手腳?


            答案是:
            1. C99的標(biāo)準(zhǔn):
            變長數(shù)組,不過支持大小為負(fù)數(shù)的數(shù)組就有些奇怪了
            Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C89 mode and in c++. (However, GCC's implementation of variable-length arrays does not yet conform in detail to the ISO C99 standard.) These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the brace-level is exited.

            2. 其實是分配在stack上面,動態(tài)的調(diào)整esp就可以了

            Feedback

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 17:51 by cexer
            確實有點古怪,那個 getnum() 明明是返回運行期數(shù)值。

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 17:51 by shaker(太子)
            真實奇思妙想啊~佩服佩服

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 17:59 by zzemu
            array的空間應(yīng)該分配在stack上。c99標(biāo)準(zhǔn)

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 18:10 by 阿福
            g++ -o main.S -S main.cpp
            這樣可以生成匯編代碼

            匯編我不太懂,通過其中調(diào)用的call __alloca大約猜到:數(shù)組被定義到堆上去了。

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 18:12 by 空明流轉(zhuǎn)
            gcc的匯編表明,那個stack是一個有著作用域的堆上變量,至于sizeof,是直接在編譯期生成了一個placehold后再運行期返回的。

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 19:02 by re
            你這是把C程序當(dāng)C++編譯,自然會有問題的.

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 19:10 by 沈臻豪(foxtail)
            暈死

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 19:35 by 過客
            傳給main的參數(shù)都錯了~~~~

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 19:43 by DraculaW
            Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C89 mode and in C++. (However, GCC's implementation of variable-length arrays does not yet conform in detail to the ISO C99 standard.) These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the brace-level is exited.

            http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 22:35 by 陳梓瀚(vczh)
            問題是為什么int Array[getnum()]居然可以編譯過去。

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 22:35 by 陳梓瀚(vczh)
            原來是新特性啊。這是好事。

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 23:25 by ZelluX
            c99的特性而已

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-13 23:52 by 踏雪赤兔
            不是很正常嗎?棧上的數(shù)組都是在運行時“分配”的,反正就加一個偏移嘛,看不出有什么不妥

            # re: 奇怪的g++的行為[未登錄]  回復(fù)  更多評論   

            2008-08-14 00:06 by jarod
            int num;
            scanf("%d", &num);
            int aaa[num];

            也可以???哪用寫個宏和函數(shù)。。。

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-14 09:49 by bugs_killer
            fuck 都亂套了..

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-21 13:19 by Icat
            應(yīng)該是
            %ud 而不是%d

            sizeof 返回的是 unsigned int,你當(dāng)作int在處理,
            一個極大的數(shù)就變成負(fù)數(shù)了

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-08-27 11:10 by abettor
            @Icat
            同意。
            我覺得也是因為變長數(shù)組把-1轉(zhuǎn)成無符號型了,但問題是,變長數(shù)組支持的長度有多大?如果沒記錯,原來數(shù)組大小貌似不能超過65535。

            # re: 奇怪的g++的行為  回復(fù)  更多評論   

            2008-09-30 12:20 by 某人
            65535 那是16位的系統(tǒng),32為系統(tǒng)是4294967295個
            精品久久久久久无码免费| 久久99九九国产免费看小说| 久久国产精品一区| 精品久久久无码中文字幕| 久久99免费视频| 国产成人无码精品久久久久免费| 久久久久久久久久久久中文字幕| 久久无码人妻一区二区三区午夜| 99久久无色码中文字幕人妻| 波多野结衣久久一区二区| 久久精品国产99国产精品导航| 久久久久亚洲av成人网人人软件 | 久久国内免费视频| 免费无码国产欧美久久18| 亚洲国产精品无码久久久秋霞2| 久久婷婷五月综合97色一本一本| 久久这里只有精品18| 久久精品九九亚洲精品天堂| 精品久久久无码中文字幕天天| 国产精品久久久久免费a∨| 中文字幕日本人妻久久久免费 | 久久久无码精品亚洲日韩蜜臀浪潮| 久久久久久久免费视频| 香蕉久久av一区二区三区| 久久99热国产这有精品| 色欲综合久久躁天天躁| 色婷婷综合久久久中文字幕| 婷婷综合久久狠狠色99h| 久久人人爽人人澡人人高潮AV | 久久精品国产亚洲av麻豆蜜芽| 7777精品久久久大香线蕉| 久久99精品国产麻豆| 国产成人久久精品麻豆一区 | 久久久久无码精品国产不卡| 四虎国产永久免费久久| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 亚洲成人精品久久| 亚洲国产天堂久久久久久| 成人免费网站久久久| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 久久97精品久久久久久久不卡|