[Linux学习]ARM中的char和X86的char的不同点
2014-01-09 16:23
225 查看
转自:http://blogger.org.cn/blog/more.asp?name=foxwolf&id=28687
对于char i=-1 打印出-1的结果
说白也就是在x86体系结构中默认的是signed char.所以打印结果是:-1
而在arm体系结构中默认的是unsigned char.所以打印的结果是:255
原因如下:
The following email fragment appeared on the linux-arm mailing list recently:
The above code is actually buggy in that it assumes that the type "char" is equivalent to "signed char". The C standards do say that "char" may either be a "signed char" or "unsigned char" and it is up to the compilers implementation or the platform which is
followed.
As the poster points out, the above code does not work as expected if "char" is "unsigned". It is difficult to detect this code at compile time, since GCC does not issue any warnings. The only way to detect it is either by visual examination of the code,
or by actually running it and finding a problem.
This causes problems on ARM based machines since "char" is of the "unsigned" variety, which allows the compiler to generate faster, more efficient code. ARM is not alone in this - SGI Mips running IRIX also encounters this problem.
However, dispite the lack of warning for the above case, GCC does warn with the following code:
Code like the above will generate a compiler warning, which will be one of the following depending on the actual test used:
Please note however that the above warnings are not issued if "char" is "signed" and therefore can be difficult to pick up when compiling in such an environment.
The following table lists the four types of code which cause problems when "c" is declared as just "char", and the most likely correct method of fixing the code.
Nos 1, 2 and 3 can cause problems even in a signed char environment. Take the instance of a file containing a byte value 0xff.
我们可以这样定义,用int8_t来代替char类型
#if !(__linux__)
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
#endif
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
文章来源:http://www.arm.linux.org.uk/docs/faqs/signedchar.php
对于char i=-1 打印出-1的结果
说白也就是在x86体系结构中默认的是signed char.所以打印结果是:-1
而在arm体系结构中默认的是unsigned char.所以打印的结果是:255
原因如下:
The following email fragment appeared on the linux-arm mailing list recently:
[code] > consider this simple program: > int main(void) > { > char i = -1; > printf("%d\n", i); > return 0; > } > > The print out is 255 in stead of -1, unless I define i as > signed char i; > then I get the "-1" print out.
The above code is actually buggy in that it assumes that the type "char" is equivalent to "signed char". The C standards do say that "char" may either be a "signed char" or "unsigned char" and it is up to the compilers implementation or the platform which is
followed.
As the poster points out, the above code does not work as expected if "char" is "unsigned". It is difficult to detect this code at compile time, since GCC does not issue any warnings. The only way to detect it is either by visual examination of the code,
or by actually running it and finding a problem.
This causes problems on ARM based machines since "char" is of the "unsigned" variety, which allows the compiler to generate faster, more efficient code. ARM is not alone in this - SGI Mips running IRIX also encounters this problem.
However, dispite the lack of warning for the above case, GCC does warn with the following code:
{ char foo; foo = bar(); if (foo == -1) { ... } }
Code like the above will generate a compiler warning, which will be one of the following depending on the actual test used:
warning: comparison is always 0 due to limited range of data type warning: comparison is always 1 due to limited range of data type
Please note however that the above warnings are not issued if "char" is "signed" and therefore can be difficult to pick up when compiling in such an environment.
The following table lists the four types of code which cause problems when "c" is declared as just "char", and the most likely correct method of fixing the code.
Code | Correction | Reasoning | |
---|---|---|---|
1. | c = getopt() | declare c as an "int" | getopt() is defined as returning an "int" not a "char" |
2. | c = getc() | declare c as an "int" | getc() is defined as returning an "int" not a "char" |
3. | c == EOF | declare c as an "int" | EOF is a "negative integral constant" |
4. | c < 0 c >= 0 | declare c as an "int" or "signed char" | this is ambiguous and examination of the surrounding code should indicate which case is more correct. However, "int" is the perferred, unless the code is relying on some characteristic of "signed char". |
我们可以这样定义,用int8_t来代替char类型
#if !(__linux__)
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
#endif
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
文章来源:http://www.arm.linux.org.uk/docs/faqs/signedchar.php
相关文章推荐
- Linux独立中断栈学习笔记及验证实验(ARM、x86)
- 在Linux中分别安装应用于不同平台的QT:PC;嵌入式X86;ARM。
- ARM中的char和X86的char的不同点
- ARM学习笔记---linux基本命令
- Linux_网蜂ARM Cortex-A8学习笔记_Linux驱动篇
- linux+ARM学习路线
- arm-linux内核启动学习笔记(一)
- 【有修改】安装ARM交叉编译器 linux x86 arm gnu工具
- arm64 linux 学习笔记二
- linux上怎么切换不同版本的arm-linux-gcc?只需改一行函数
- arm-linux-ld命令 ld链接脚本(不错,推荐可以学习一些lds链接脚本文件)
- ARM-LINUX学习笔记-1
- 嵌入式ARM+Linux学习记录(3)-u-boot移植
- 【嵌入式Linux学习七步曲之第二篇 ARM+Linux开发环境】gdb+gdbserver的方式进行ARM程序调试
- linux arm和x86 inline hook技术
- StrongMouse学习ARM-Linux开发(1)
- 为何 Linux 之父 Linus Torvalds 在 ARM 会议上说他喜欢 x86
- 【Linux学习】Ubuntu下嵌入式交叉编译环境arm-linux-gcc搭建
- ARM-LINUX学习笔记
- 我的内核学习笔记13:x86平台linux系统重启流程跟踪