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

             

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

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

            如果你正在寫從文件或網(wǎng)絡讀寫數(shù)據(jù)的跨平臺C/C++代碼,那么你必須明白有些問題是因語言,編譯器,平臺而不同的。 主要的問題是數(shù)據(jù)對齊,填充類型大小字節(jié)順序默認狀態(tài)char是否有符號
            對齊
            特定機器上,特定的數(shù)據(jù)被對齊于特定的邊界。如果數(shù)據(jù)沒有正確對齊,結(jié)果可能是效率降低甚至崩潰。 當你從I/O源讀取數(shù)據(jù)的時候,確保對齊是正確的。詳細內(nèi)容參考本人另一篇blog: 字節(jié)對齊的影響因素
            填充
            "填充" 是數(shù)據(jù)集合中不同元素之間的間隔, 一般是為了對齊而存在。不同編譯器和平臺下,填充的數(shù)量可能會不同。 不要假設結(jié)構(gòu)的大小和成員的位置在任何編譯器和平臺下都是相同的。 不要一次性讀取或者寫入一整個結(jié)構(gòu)體,因為寫入的程序可能會使用和讀取的程序不同的填充方式。對于域也同樣適用。
            類型大小
            不同數(shù)據(jù)類型的大小隨編譯器和平臺而不同。 在C/C++中, 內(nèi)置類型的大小完全取決于編譯器(在特定范圍內(nèi)). 不要讀寫大小不明確的數(shù)據(jù)類型。也就是說,不要讀寫bool, enum, long, int, short, float, 或者double類型.(譯者注:linux下要使用下面跨平臺符號,要加載頭文件<arpa/inet.h>,此外在C99已經(jīng)增加了一個頭文件stdint.h,支持標準的,可移植的整數(shù)類型集合,此文件被包含在<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 位環(huán)境中,而 LP64 用于 64 位環(huán)境中。這兩種模型之間的區(qū)別在于長整型和指針的大小。
            系統(tǒng)中可以有兩種不同的數(shù)據(jù)類型:基本數(shù)據(jù)類型和衍生數(shù)據(jù)類型。
            基本數(shù)據(jù)類型是 C C++ 語言規(guī)范定義的所有數(shù)據(jù)類型。下表對 Linux on POWER Solaris 中的基本數(shù)據(jù)類型進行了比較:
            4:基本數(shù)據(jù)類型
             
            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. 衍生數(shù)據(jù)類型
            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
             
            字節(jié)順序
            字節(jié)順序,就是字節(jié)在內(nèi)存中存儲的順 序。 不同的處理器存儲多字節(jié)數(shù)據(jù)的順序是不同的。小端處理器由低到高存儲(換句話說,和書寫的順序相反).。大端處理器由高到低存儲(和書寫順序相同)。如果 數(shù)值的字節(jié)順序和讀寫它的處理器不同,它必須被事先轉(zhuǎn)化。同時,為了標準化網(wǎng)絡傳輸?shù)淖止?jié)順序,定義了網(wǎng)絡字節(jié)順序。詳細內(nèi)容參考本人另一篇blog:  網(wǎng)絡通訊中字節(jié)排列順序轉(zhuǎn)化
            char - 有符號還是無符號?
            一個鮮為人知的事實,char默認可以是有符號的也可以是無符號的-完全取決于編譯器。結(jié)果導致你從char轉(zhuǎn)化為其他類型的時候(比如int),結(jié)果會因編譯器而不同。 例如:

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

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

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

            導航

            統(tǒng)計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久久精品妇女99| 久久久精品视频免费观看| 日本精品久久久久中文字幕8| 国产精品乱码久久久久久软件| 青青青国产精品国产精品久久久久| 伊人久久大香线蕉综合影院首页| 久久国产美女免费观看精品 | 97精品国产97久久久久久免费| 91精品国产综合久久香蕉| 2020久久精品国产免费| 久久亚洲AV成人无码国产| 亚洲国产欧美国产综合久久| 久久久久人妻一区二区三区| 午夜精品久久久久久影视riav| 精品久久久久久久国产潘金莲| 亚洲国产另类久久久精品| 色欲久久久天天天综合网精品| 久久综合精品国产二区无码| 久久久久久久久久久久中文字幕| 久久发布国产伦子伦精品| 精品少妇人妻av无码久久| 国产韩国精品一区二区三区久久| 久久精品成人免费网站| 久久国产免费直播| 中文精品99久久国产| 精品久久无码中文字幕| 国产精品99久久久久久www| 色天使久久综合网天天| 99久久久精品免费观看国产| 26uuu久久五月天| 国产精品中文久久久久久久| 国产一久久香蕉国产线看观看| 国产伊人久久| 亚洲精品蜜桃久久久久久| 91精品免费久久久久久久久| 少妇被又大又粗又爽毛片久久黑人| 国产毛片欧美毛片久久久| 国产69精品久久久久99| 波多野结衣AV无码久久一区| 精品久久人人做人人爽综合| 久久久久久久久久久久久久|