Why do C data types have these specific sizes on this machine?

I noticed that the sizes of D data types can vary . I am wondering what causes the specific dimensions that this device 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 

This is because my car:

  • Linux?
  • 64 bit?
  • Ubuntu?
  • Old?
  • Desktop distribution? (unlike a mobile phone, that is, Android).
  • Full eels?
  • Using an Intel processor?
  • Using GCC to compile?
  • Something else?

Here are the system data:

 $ 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 ... 

To clarify, I do not mean that something is wrong with the system. Rather, I would like to know why the values โ€‹โ€‹are what they are, so that I can expect which systems they will be different so that I can write portable code.

0
source share
2 answers

The model has LP64 , which means that pointers are also long 64-bit; This is the standard for AMD64 on Linux and many other platforms; the other is Windows LLP64 , where only long long is 64 bits wide and long is 32 bits wide.

Common choice: char is the smallest addressable unit and preferably 8 bits. sizeof(short) at least 2; if the processor supports this, then so be it. int usually chosen as the fastest integer type - on the AMD64 architecture in 64-bit mode, 32-bit registers are faster or more supported than 16 or 64-bit registers (for 16 and 64-bit registers, a byte prefix is โ€‹โ€‹required).

Now, the reason Windows uses LLP64 is because compatibility - a lot of code made the wrong assumption that long is 32 bits; similarly in the Unix world, it is assumed that the pointer matches long - now that the pointers are 64-bit, then long should match that width.


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

+2
source

I think itโ€™s safe to say, from a practical point of view, the processor is likely to be the driving force.

The appropriate architecture for the processor will undoubtedly affect the resizing int, but if you target more than one specific processor (say, Intel i3 530), then you will need to keep that in mind.

32-bit Intel Pentium versus 64-bit Intel Pentium.

Hand 9 in the phone against the 11th bracket in the POS countertop.

and etc.

One way to solve this problem is to use int32_t, uint64_t and friends. Where possible, they will have the same size: 32 bits for uint32_t, etc.

+3
source

Source: https://habr.com/ru/post/984413/


All Articles