为什么ç数据类型有这台机器上,这些具体的尺寸? [英] Why do C datatypes have these specific sizes on this machine?

查看:164
本文介绍了为什么ç数据类型有这台机器上,这些具体的尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,ç数据类型大小可能会有所不同。我不知道是什么原因导致这台机器产生的具体尺寸:

I notice that C datatypes' sizes may vary. I wonder what causes the specific sizes that this machine produces:

$ cat sizes.c
int main()
{
    printf("void *:%ld\n", sizeof(void *));
    printf("char:%ld\n", sizeof(char));
    printf("short:%ld\n", sizeof(short));
    printf("int:%ld\n", sizeof(int));
    printf("long:%ld\n", sizeof(long));
    printf("long long:%ld\n", sizeof(long long));
    printf("float:%ld\n", sizeof(float));
    printf("double:%ld\n", sizeof(double));
    printf("long double:%ld\n", sizeof(long double));

    return 0;
}

$ ./sizes
void *:8
char:1
short:2
int:4
long:8
long long:8
float:4
double:8
long double:16

这是因为我的机器是:

Is this because my machine is:


  • Linux呢?

  • 64位?

  • Ubuntu的?

  • 老?

  • 桌面发行版? (相对于移动,即,Android的)

  • 鳗鱼满了吗?

  • 使用英特尔处理器?

  • 使用GCC编译?

  • 别的东西吗?

下面是系统的详细信息:

Here are the system details:

$ uname -a
Linux melancholy 3.13.0-46-generic #77-Ubuntu SMP Mon Mar 2 18:23:39 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/issue
Ubuntu 14.04.2 LTS \n \l

$ cat /proc/version
Linux version 3.13.0-46-generic (buildd@tipua) (gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) ) #77-Ubuntu SMP Mon Mar 2 18:23:39 UTC 2015

$ cat /proc/cpuinfo 
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 60
model name      : Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz
stepping        : 3
microcode       : 0x12
cpu MHz         : 3201.000
cache size      : 6144 KB
physical id     : 0
siblings        : 4
core id         : 0
cpu cores       : 4
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid
bogomips        : 6400.67
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:

... Another three identical processors ...

要澄清一下,我不是暗示有什么不对的系统。相反,我想知道为什么值是它们是什么,这样我可以预见什么系统,他们将是不同的,这样我可以写便携式code。

To clarify, I'm not implying that there is anything wrong with the system. Rather, I would like to know why the values are what they are so that I could anticipate on what systems they will be different so that I could write portable code.

推荐答案

该模式存在的 LP64 ,这意味着指针和长为64位;这是AMD64 Linux上,还有更多平台的标准;另一个是在Windows的 LLP64 ,其中只有长长为64位宽和为32位。

The model there is LP64, meaning pointers and long are 64-bits; this is a standard for AMD64 on Linux and many more platforms; the other being the Windows LLP64, where only long long is 64 bits wide and long is 32 bits.

通常的选择是:字符是最小的可寻址单元和preferably 8位。 的sizeof(短)至少为2;如果处理器支持这一点,那就这样吧。 INT 通常选择是的最快的整数类型 - 在64位模式32位寄存器AMD64架构是更快地使用,或更多支持大于16或64位的寄存器(16位和64位寄存器模式需要preFIX字节)。

The usual choices are: char is the smallest addressable unit and preferably 8 bits. sizeof(short) is at least 2; if the processor supports this, then so be it. int is usually chosen to be the fastest integer type - on AMD64 architecture in 64-bit mode 32-bit registers are faster to use, or more supported than 16 or 64-bit registers (the 16 and 64 bit register modes require prefix bytes).

现在为什么Windows使用的原因 LLP64 是兼容性 - 很多code,使得这病假设是32位;同样,在Unix世界中,它已被假定指针适合长期 - 现在的三分球是64位的,那么必须与宽度相匹配。

Now the reason why Windows uses LLP64 is that of compatibility - lots of code has made the ill assumption that long is 32 bits; similarly in Unix world, it has been assumed that a pointer fits in long - now that the pointers are 64-bits, then the long must match that width.

要编写可移植的程序,包括< inttypes.h> 和使用常量那里;否则假定这些类型是其最小尺寸(中间体2字节等)。使用使用intptr_t / uintptr_t形式整数变量也应宽到足以容纳一个指针 - 或者使用一个联盟。

To write portable programs, include the <inttypes.h> and use the constants there; otherwise assume that these types are of their minimum size (int 2 bytes, etc). Use intptr_t/uintptr_t for integer variables that should also be wide enough to hold a pointer - or use an union.

这篇关于为什么ç数据类型有这台机器上,这些具体的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆