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

            小明思考

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

            奇怪的g++的行為

            Posted on 2008-08-13 16:56 小明 閱讀(3638) 評論(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++標準么?連sizeof成了運行期計算
            2.這個array的空間應該分配在heap上,而不是stack上。g++做了什么手腳?


            答案是:
            1. C99的標準:
            變長數組,不過支持大小為負數的數組就有些奇怪了
            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上面,動態的調整esp就可以了

            Feedback

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

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

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

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

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

            2008-08-13 17:59 by zzemu
            array的空間應該分配在stack上。c99標準

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

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

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

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

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

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

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

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

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

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

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

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

            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++的行為  回復  更多評論   

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

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

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

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

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

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

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

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

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

            也可以啊?哪用寫個宏和函數。。。

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

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

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

            2008-08-21 13:19 by Icat
            應該是
            %ud 而不是%d

            sizeof 返回的是 unsigned int,你當作int在處理,
            一個極大的數就變成負數了

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

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

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

            2008-09-30 12:20 by 某人
            65535 那是16位的系統,32為系統是4294967295個
            色综合久久无码中文字幕| 久久精品9988| 亚洲欧美伊人久久综合一区二区 | 亚洲精品综合久久| 久久久久亚洲Av无码专| 国产99久久九九精品无码| 久久午夜福利无码1000合集| 潮喷大喷水系列无码久久精品| 久久婷婷色综合一区二区| 国产精品久久久久久| 一本久久a久久精品亚洲| 岛国搬运www久久| 国产精品久久久久久一区二区三区| 一级做a爰片久久毛片免费陪| 熟妇人妻久久中文字幕| 香蕉久久久久久狠狠色| 久久综合久久久| 99久久成人国产精品免费| 狠狠色丁香久久婷婷综合| 久久久久久亚洲精品影院| 99久久精品国产综合一区| 狠狠色丁香婷婷综合久久来| 久久中文字幕精品| 久久中文字幕人妻丝袜| 久久亚洲中文字幕精品一区| 久久天天躁狠狠躁夜夜2020| 天天久久狠狠色综合| 精品午夜久久福利大片| 久久久久久久久无码精品亚洲日韩 | 久久国产一片免费观看| 久久久久久免费一区二区三区| 久久久久久国产精品免费无码| 国产69精品久久久久9999APGF | 久久精品国产免费一区| 久久久九九有精品国产| 91精品国产高清久久久久久国产嫩草| 久久精品国产亚洲AV无码偷窥 | 久久久无码精品亚洲日韩软件| 久久久久国产成人精品亚洲午夜| 狠狠久久综合伊人不卡| 青青热久久国产久精品|