我在Windows上做C++开发与Visual Studio编译器,专门的Visual Studio 2015年更新3C++之间的差异无符号整型和unsigned long int类型
对于我使用unsigned int类型/无符号的一些DSP相关工作长数据类型。我想知道这两种内置C/C++类型有什么区别。
我通过Google和SO搜索了一下,发现了这些引用。
- Types documentation上cppreference.com
- Types documentation MSDN上的Visual Studio 2015年
- Types documentation的GNU C/C++(如G++ compiler指出,C/C++使用相同的默认类型的实现,我指到C DOC这里)
我假定cppreference文档是ISO C++ 11标准的总结。因此,从 “标准” 无符号和unsigned int类型是依赖于LP/ILP 32/64数据模型16/32位,而unsigned long类型和unsigned long int类型是依赖于LP 32/64比特/ ILP 32/64数据模型。
对于MSDN和GNU文档他们都表示,unsigned int类型/unsigned long类型使用的是32位的实现,可容纳价值高达4,294,967,295。然而,GNU文档还指出,根据你的系统,unsigned long可以是64位,它与unsigned long long int相同。
那么,如下我的问题:
- 对于无符号长64位,被超越上限4,294,967,295一个未定义的行为或正确的行为?
- 如果我有一个应用程序的Windows系统在Visual Studio编译的有关工作,basicall 无符号 == unsigned long类型。对或错?
- 如果我有工作在Linux/Windows的由GNU编译器编译应用程序我必须确保是否unsigned long类型 == unsigned int类型或unsigned long类型 == 无符号长长,以避免数据溢出。对或错
- 如果我有一个跨平台的应用程序,可能是由所有这些Visual Studio/GNU/Clang/Intel编译器编译的,我必须用一堆预处理器清楚地分类环境,以避免数据溢出。对或错
在此先感谢。
编辑: 感谢@PeterRuderman指出超出ceil值为无符号类型不是未定义的行为。
然后我的问题1将变更为:
- 对于无符号长64位,将超出上限4,294,967,295使得其自身环绕?
请注意,即使'unsigned int'和'unsigned long'碰巧具有相同的范围和表示形式,它们仍然是不同的类型。 –