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

            colorful

            zc qq:1337220912

             

            跨平臺開發連載(6_跨平臺IO的影響因素)

            http://blog.csdn.net/wqf363/article/details/1420554

            如果你正在寫從文件或網絡讀寫數據的跨平臺C/C++代碼,那么你必須明白有些問題是因語言,編譯器,平臺而不同的。 主要的問題是數據對齊,填充類型大小字節順序默認狀態char是否有符號
            對齊
            特定機器上,特定的數據被對齊于特定的邊界。如果數據沒有正確對齊,結果可能是效率降低甚至崩潰。 當你從I/O源讀取數據的時候,確保對齊是正確的。詳細內容參考本人另一篇blog: 字節對齊的影響因素
            填充
            "填充" 是數據集合中不同元素之間的間隔, 一般是為了對齊而存在。不同編譯器和平臺下,填充的數量可能會不同。 不要假設結構的大小和成員的位置在任何編譯器和平臺下都是相同的。 不要一次性讀取或者寫入一整個結構體,因為寫入的程序可能會使用和讀取的程序不同的填充方式。對于域也同樣適用。
            類型大小
            不同數據類型的大小隨編譯器和平臺而不同。 在C/C++中, 內置類型的大小完全取決于編譯器(在特定范圍內). 不要讀寫大小不明確的數據類型。也就是說,不要讀寫bool, enum, long, int, short, float, 或者double類型.(譯者注:linux下要使用下面跨平臺符號,要加載頭文件<arpa/inet.h>,此外在C99已經增加了一個頭文件stdint.h,支持標準的,可移植的整數類型集合,此文件被包含在<inttypes.h>)
            用這些
            替代這些...
            int8, uint8
            char, signed char, unsigned char, enum, bool
            int16, uint16
            short, signed short, unsigned short, enum
            int32, uint32
            int, signed int, unsigned int, long, signed long, unsigned long, enum
            int64, uint64
            long, signed long, unsigned long
            int128, uint128
            long long, signed long long, unsigned long long
            float32
            float
            float64
            double
             
            Data Type Ranges
            C/C++ recognizes the types shown in the table below.
            Type Name
            Bytes
            Other Names
            Range of Values
            int
            *
            signed,
            signed int
            System dependent
            unsigned int
            *
            unsigned
            System dependent
            __int8
            1
            char,
            signed char
            –128 to 127
            __int16
            2
            short,
            short int,
            signed short int
            –32,768 to 32,767
            __int32
            4
            signed,
            signed int
            –2,147,483,648 to 2,147,483,647
            __int64
            8
            none
            –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
            char
            1
            signed char
            –128 to 127
            unsigned char
            1
            none
            0 to 255
            short
            2
            short int,
            signed short int
            –32,768 to 32,767
            unsigned short
            2
            unsigned short int
            0 to 65,535
            long
            4
            long int,
            signed long int
            –2,147,483,648 to 2,147,483,647
            unsigned long
            4
            unsigned long int
            0 to 4,294,967,295
            enum
            *
            none
            Same as int
            float
            4
            none
            3.4E +/- 38 (7 digits)
            double
            8
            none
            1.7E +/- 308 (15 digits)
            long double
            10
            none
            1.2E +/- 4932 (19 digits)
             
                The long double data type (80-bit, 10-byte precision) is mapped directly to double (64-bit, 8- byte precision) in Windows NT and Windows 95.
                Signed and unsigned are modifiers that can be used with any integral type. The char type is signed by default, but you can specify /J to make it unsigned by default.
                The int and unsigned int types have the size of the system word. This is two bytes (the same as short and unsigned short) in MS-DOS and 16-bit versions of Windows, and 4 bytes in 32-bit operating systems. However, portable code should not depend on the size of int.
                Microsoft C/C++ also features support for sized integer types. See __int8, __int16, __int32, __int64 for more information. Also see Integer Limits.
                   此外,顯示個32位與64位平臺之間的差異示例:
            對于 Linux on POWERILP 32 模型用于 32 位環境中,而 LP64 用于 64 位環境中。這兩種模型之間的區別在于長整型和指針的大小。
            系統中可以有兩種不同的數據類型:基本數據類型和衍生數據類型。
            基本數據類型是 C C++ 語言規范定義的所有數據類型。下表對 Linux on POWER Solaris 中的基本數據類型進行了比較:
            4:基本數據類型
             
            Linux on POWER
            Solaris
            基本類型
            ILP32
            LP64
            ILP32
            LP64
            char
            8
            8
            8
            8
            short
            16
            16
            16
            16
            init
            32
            32
            32
            32
            float
            32
            32
            32
            32
            long
            32
            64
            32
            64
            pointer
            32
            64
            32
            64
            long long
            64
            64
            64
            64
            double
            64
            64
            64
            64
            long double
            64/128*
            64/128*
            128
            128
            5. 衍生數據類型
            OS
            gid_t
            mode_t
            pid_t
            uid_t
            wint_t
            Solaris ILP32 l
            long
            unsigned long
            long
            long
            long
            Solaris LP64
            int
            unsigned int
            int
            int
            int
            Linux ILP32
            unsigned int
            unsigned int
            int
            unsigned int
            unsigned int
            Linux ILP64
            unsigned int
            unsigned int
            int
            unsigned int
            unsigned int
             
            字節順序
            字節順序,就是字節在內存中存儲的順 序。 不同的處理器存儲多字節數據的順序是不同的。小端處理器由低到高存儲(換句話說,和書寫的順序相反).。大端處理器由高到低存儲(和書寫順序相同)。如果 數值的字節順序和讀寫它的處理器不同,它必須被事先轉化。同時,為了標準化網絡傳輸的字節順序,定義了網絡字節順序。詳細內容參考本人另一篇blog:  網絡通訊中字節排列順序轉化
            char - 有符號還是無符號?
            一個鮮為人知的事實,char默認可以是有符號的也可以是無符號的-完全取決于編譯器。結果導致你從char轉化為其他類型的時候(比如int),結果會因編譯器而不同。 例如:

            char   x;
            int    y;
            read( fd, &x, 1 );   // 讀取一個byte值為0xff
            y = x;               // y 是 255 或者 -1, 依賴編譯器

            不要把數據讀入一般的char。明確指定是有符號或者無符號的

            posted on 2012-05-19 09:51 多彩人生 閱讀(448) 評論(0)  編輯 收藏 引用

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲人成网站999久久久综合 | 国内精品人妻无码久久久影院| 亚洲欧美国产日韩综合久久| 国产69精品久久久久9999APGF| 久久久久亚洲精品无码蜜桃| 99久久成人国产精品免费| 久久中文字幕一区二区| 亚洲精品tv久久久久久久久久| 亚洲AV无码久久| 久久久久99精品成人片三人毛片| 伊人色综合久久天天人手人婷 | 亚洲国产成人久久综合一区77 | 国产精品久久久久影院嫩草| 久久99精品国产麻豆婷婷| 久久人人爽人人爽人人av东京热| 97精品久久天干天天天按摩| 亚洲欧美日韩精品久久亚洲区| 久久精品人人槡人妻人人玩AV| 精品久久人人爽天天玩人人妻| 天天爽天天狠久久久综合麻豆| 亚洲精品tv久久久久久久久久| 青青草原1769久久免费播放| 久久久亚洲欧洲日产国码二区 | 99久久99久久| 日韩精品久久无码中文字幕| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 亚洲欧洲久久久精品| 久久国产精品偷99| 精品精品国产自在久久高清| 久久久久久亚洲AV无码专区| 精品国产乱码久久久久软件| 伊人久久大香线蕉精品不卡 | 久久中文娱乐网| 久久香蕉国产线看观看乱码| 国产精品久久久久jk制服| 久久亚洲精品国产精品| 久久夜色精品国产噜噜噜亚洲AV| 伊人色综合九久久天天蜜桃| 综合久久精品色| 99久久国产综合精品女同图片| 国产成人无码精品久久久性色|