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.
此外,顯示個32位與64位平臺之間的差異示例:
對于 Linux on POWER,ILP 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。明確指定是有符號或者無符號的