跨平台开发影响的因素

来源:互联网 发布:淘宝网婴儿床 编辑:程序博客网 时间:2024/05/04 19:29
 

如果你正在写从文件或网络读写数据的跨平台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。明确指定是有符号或者无符号的

原创粉丝点击