n******7 发帖数: 12463 | 1 golang的int是个alias
32bit机器上是int32
64bit机器上是int64
这不是个问题吗?
比如我只想用int32,用了int在64bit机器上内存都double了?
golang的tutorial说
“The int, uint, and uintptr types are usually 32 bits wide on 32-bit
systems and 64 bits wide on 64-bit systems. When you need an integer value
you should use int unless you have a specific reason to use a sized or
unsigned integer type. ”
我试过坚持用int32,结果老是得转换类型,后来放弃了 | n******7 发帖数: 12463 | 2 关于C/C++的int,找到一个讨论:
就是说不同size的 int确实confusing?
https://stackoverflow.com/a/2331768
Yes, it depends on both processors (more specifically, ISA, instruction set
architecture, e.g., x86 and x86-64) and compilers including programming
model. For example, in 16-bit machines, sizeof (int) was 2 bytes. 32-bit
machines have 4 bytes for int. It has been considered int was the native
size of a processor, i.e., the size of register. However, 32-bit computers
were so popular, and huge number of software has been written for 32-bit
programming model. So, it would be very confusing if 64-bit computer would
have 8 bytes for int. Both Linux and Windows remain 4 bytes for int. But,
they differ in the size of long.
Please take a look at the 64-bit programming model like LP64 for most *nix
and LLP64 for Windows:
http://www.unix.org/version2/whatsnew/lp64_wp.html
http://en.wikipedia.org/wiki/64-bit#64-bit_data_models
Such differences are actually quite embarrassing when you write code that
should work both on Window and Linux. So, I'm always using int32_t or int64_
t, rather than long, via stdint.h. |
|