[Linux学习]ARM中的char和X86的char的不同点
来源:互联网 发布:行业智能和大数据 编辑:程序博客网 时间:2024/05/24 07:03
转自: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:
> 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 typewarning: comparison is always 1 due to limited range of data typePlease 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.
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".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
- [Linux学习]ARM中的char和X86的char的不同点
- ARM中的char和X86的char的不同点
- char和string的不同点
- c++中的char* 和char[]的使用
- linux 下的unsigned char 和char
- arm-linux-gcc char与signed char和unsigned char
- char* 和 char[]的区别:
- char * 和 char []的区别
- char *和char [ ]的区别
- char* 和char[]的区别
- char* 和char[]的区别
- char * 和 char []的区别
- char* 和char[]的区别
- char* 和char[]的区别
- char* 和 char[]的区别
- char* 和char[]的区别
- char* 和char[]的区别
- char* 和char[]的区别
- HEX文件和BIN文件格式的区别
- Part-time Job
- 不用Cookie的仿刷新二级高亮菜单
- 关于ios中BLE4.0的精度问题
- mysql数据库创建,表创建,插入数据
- [Linux学习]ARM中的char和X86的char的不同点
- 关于Android原生支持Gif动态图的问题
- 如何在 Ubuntu 12.04 Server 中安装图形用户界面
- A+B for Input-Output Practice1(入门)
- 第1周 Hadoop的源起与体系介绍
- 《大数据时代》读书笔记
- 叠片过滤器工作原理及技术应用指南
- 拼音首字母检索
- 苹果消息推送正式环境