Hello, I looked at the different data types and compared their range and consumed memory, and contradictions arose. Exactly:

  • short and int have the same range, but different weights are -2 and 4 bytes, respectively.
  • long and int have the same "weight" - 4 and 4 bytes, respectively, but a different range.

PS measured using sizeof() , data on the range from Wikipedia.

So why such differences, if I measured everything correctly, the int type should not be used at all, since it has a lighter analog — a short and a more capacious analog (for the memory used) long .

Please help understand. Thank you in advance.

  • one
    Integer types of different sizes cannot have the same range. You measured something wrong. - gkuznets
  • typeof (<variable>) returns the data type of the variable. Used to organize function templates and classes. - user208051
  • In c ++ there is no typeof . - αλεχολυτ
  • Here is a link to an article in Wikipedia about data types in C ++: ru.m.wikipedia.org/wiki ... and there, in my opinion, it is clearly written that the data sizes are not governed by the standard but depend on the platform. And with the example of the same int and short, they write that at least the int is short, but in practice it is 4 bytes with a range greater than 4 billion (unsigned). And if you measure the size, then do not be lazy to measure and range - you will see that the range is entirely dependent on the size. - Andrej Levkovitch

5 answers 5

Data on ranges is better not to get from Wikipedia, but from std :: numeric_limits

 #include <limits> cout<<std::numeric_limits<int>::max(); // вывод макс. значения int 

Do it on your machine and compare the results.

  • only not <numeric_limits>, but simply <limits> - gkuznets
  • I did, as you said, c short and int were defined, and int and Long were identical :) - username76
  • By the way, I was always interested in whether it is possible to define data types independently in C ++, that is, their range (in order to save memory). - username76
  • C ++ 11 defines the header file <cstdint> (if there is none, you can try <stdint.h>) in which the types int8_t, int16_t, int32_t, int64_t and acc. unsigned uint8_t, uint16_t, uint32_t, uint64_t. Using them you can be sure of the exact size of the data. - gkuznets
  • Please give a link to the detailed description. - username76

Type sizes and ranges are not specified in the standard and may depend on the platform. It is only guaranteed that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) .

You can read about types here in the "Fundamental data types" section.

  • That is, in my case, use short instead of int? - username76
  • Usually short is an integer from -32768 to 32767. - andrybak

This is some minus of C and C ++ - the fact that on different platforms the same types of variables have different sizes and, accordingly, different data ranges. The standard defined only the minimum. When writing portable code, this creates some difficulties, you need to somehow excel.

  • no need to excel, need to use <cstdint> - gkuznets
  • Sometimes this is the only way to create a guaranteed portable code. But I attribute this to a number of perversions)) Normally, standard types should be portable. - skegg
  • So this is the standard header file that defines the standard types. - gkuznets
  • Each system will have its own overrides. Programs should not slow down, everything is in any case given to word, dword, etc. still at compilation stage. - skegg 4:02 pm
  • @mikillske section 5.2.4.2.1 Sizes of integer types <limits.h>. Otherwise, it can be understood as the requirement to have one processor for all occasions - alexlz

char - almost always 1 byte. signed char has a range [-128; 127], unsigned char - [0; 255]. Simply char by default depends on the compiler and its settings. It can be both signed and unsigned. The remaining types are always by default signed.

short has size 2. Range [-32768; 32767], unsigned short - [0; 65535].

int on 16-bit processors is similar to short, on all others it is 4 bytes in size. Range [-2147483648; 2147483647], unsigned - [0; 4294967295].

long is almost always the same as int, but on linux x64 it is 8 bytes in size.

long long has a size of 8 bytes, but I don’t know what will happen when 128-bit processors come out. Ranges will not write, but they are huge.

    The types short , int , long and long long do not have a standard size. As a rule, their size is determined based on the processor architecture. For example, on x86 long - 4 bytes, on x86_64 - 8 bytes.

    There is another important feature in the definition of these types. C and C ++ languages ​​were created to work on as many platforms as possible. Some platforms do not support memory without alignment to the size of the machine word . The long type is the standard type of machine word in C and C ++. On such platforms, the memory address should always be a multiple of sizeof(long) , and most often, it is this minimum byte that we can read from memory at a time.

    Although x86 and followers support unaligned memory access, aligned access is usually still faster, so similar long notation is still important.

    To save people from contradictions, fixed-size types were introduced, for example, int32_t , uint8_t , int64_t . In most cases, when writing portable programs, the priority of using types should be:

    • Special types ( size_t , off_t ). We apply where they semantically express the essence of variables.

    • Types with a fixed size. So we avoid different problems when compiling 32 and 64 bits, especially when working with files and the network.

    • We use classical types ( int , short , long ) for platform-specific optimizations, and remember that the size may change on a different platform.