long数据类型跨平台问题
源代码
#include <iostream>
int main()
{
std::cout << "size of long : " << sizeof(long) << std::endl;
return 0;
}
Windows
long -> 32位
Linux
long -> 64位
建议
跨平台程序尽量采用跨平台库,如Boost
namespace boost
{
using ::int8_t;
using ::int_least8_t;
using ::int_fast8_t;
using ::uint8_t;
using ::uint_least8_t;
using ::uint_fast8_t;
using ::int16_t;
using ::int_least16_t;
using ::int_fast16_t;
using ::uint16_t;
using ::uint_least16_t;
using ::uint_fast16_t;
using ::int32_t;
using ::int_least32_t;
using ::int_fast32_t;
using ::uint32_t;
using ::uint_least32_t;
using ::uint_fast32_t;
# ifndef BOOST_NO_INT64_T
using ::int64_t;
using ::int_least64_t;
using ::int_fast64_t;
using ::uint64_t;
using ::uint_least64_t;
using ::uint_fast64_t;
# endif
using ::intmax_t;
using ::uintmax_t;
}
或者stdint.h
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
typedef signed char int_least8_t;
typedef short int_least16_t;
typedef int int_least32_t;
typedef long long int_least64_t;
typedef unsigned char uint_least8_t;
typedef unsigned short uint_least16_t;
typedef unsigned int uint_least32_t;
typedef unsigned long long uint_least64_t;
typedef signed char int_fast8_t;
typedef int int_fast16_t;
typedef int int_fast32_t;
typedef long long int_fast64_t;
typedef unsigned char uint_fast8_t;
typedef unsigned int uint_fast16_t;
typedef unsigned int uint_fast32_t;
typedef unsigned long long uint_fast64_t;
typedef long long intmax_t;
typedef unsigned long long uintmax_t;