C ++ - What's the difference between using fixed types of C ++ and typedef of a type?

11

A few days ago studying emulation I came across a stack overflow question in English on the emulation of CHIP8, and one of the responses told me not to use C arrays such as int i[12] instead of using std::array to be a fixed type, this left me a bit confused as to what a fixed type would be, another example would be a byte type that normally people create with typedef unsigned char BYTE , in C ++ it has the equivalent std::uint8_t only opening its definition in the visual studio I came across this typedef unsigned char uint8_t; . So what's the difference between using the declared C ++ type or using your own user-defined type if both are the same?

    
asked by anonymous 12.11.2018 / 12:03

3 answers

9

It seems that the question boils down to "what are the advantages of using fixed types of the standard library (e.g., std::uint8_t ) instead of non-fixed types (e.g. unsigned char )."

Well, the advantages are (the following is not exhaustive):

  • The size in bits remains the same in any environment. You know that an integer std::uint8_t will always have 8 bits, regardless of which environment (i.e., the platform) the code is compiled and executed. In the case of type like char , its only guarantee is that sizeof(char) == 1 , and that sizeof(char) <= sizeof(short) , nothing else. There is no information about the bit size of these types, and they can vary from environment to environment. This is the number one problem fixed types solve. Implementing these types can take advantage of the normal types, as you've already noted. Implementation does not matter, what matters is the assurances and implications of fixed size types. Addendum: These fixed-size types of the standard C ++ library are optional, so it is possible that an implementation does not provide them (in practice this is unlikely nowadays).
  • Everyone knows these types and knows their implications. Since these fixed types are in C ++ standardization, it is assumed that the programmer knows its use cases, implications and behaviors. If the programmer defines a new type, another programmer will not know of its existence, so he needs to learn about all the points I've listed. This creates unnecessary friction, since already known types already exist in standardization precisely because of this.
  • Specific problems require specific types. The type std::size_t is used to store all possible values of sizeof in an environment. The type std::ptrdiff_t is the result type of the result of subtracting two pointers, and is used for pointer arithmetic and array indexing. The type std::max_align_t has the property of having its alignment as large as any other scalar type. The type std::byte represents raw byte values. The type std::intmax_t is the largest type in bits. The type std::int32_t ensures that it is 32 bits in size, has no padding and uses 2's complement for negative numbers. Anyway, each type has its implications, use cases etc.

You can see all fixed size types here: link . Some other types are here: link

    
12.11.2018 / 13:42
6

The difference between an array in C and a std::array is basically the interface of it. The std::array has a container interface (like std::vector , for example), so you have a number of features, such as knowing the size of the array, being able to use it in STL algorithms (since it has begin and end ), limits checking, and also if in the future you need to switch to a dynamic allocation container, just switch to std::vector and all your code will work as before. Finally, in terms of performance the two should have the same performance.

As for typedef , it just creates a nickname for the type, but in the end it's the same type. It is usually used to give meaning to the type. But prefer the using which is the equivalent in C ++. typedef exists only for C compatibility.

typedef unsigned char BYTE; // C
using BYTE = unsigned char; // C++ 
    
12.11.2018 / 12:21
6

Without seeing the context, it is a bit difficult to answer, including because it is common for the person explaining what he has read wrong.

I believe I'm just talking about a fixed array type, such as the array of C. It would be an opposition to an array which can be expanded if necessary. You can view more at Difference between std :: list, std :: vector and std :: array .

The question got pretty confusing and what it seemed to ask was not quite what was shown after the comment gave context. The information is still there because it can be useful to someone but what was asked was something else. In the text that was used to generate the doubt, it does not have the term fixed type (fixed type) as shown in the question, it speaks of fixed size type, which is quite different, in> array in the question here has nothing to do with the doubt, it was just about array in question there in Code SE Review. We were misled.

Doubt is about the types that have guaranteed quantity bits. Original in C the primary data types were specified only with minimum size and each implementation of the C language compiler could choose the actual size according to the architecture that it would generate the code, which could give better efficiency. Over time it has been found that in many cases it would be interesting to have types where one knows the exact size to be used. C and C ++ adopted this, but the default was still the exact size type not specified in advance. So these types that came later in the language are called fixed-size types.

It is not recommended to use typedef in modern C ++, it is better to use using that the compiler is better prepared, typedef still works because of the legacy. in this context it is a nickname of types.

Typically, type aliases are used for the sake of readability (to show intention) and to give some flexibility, so you use an abstract type and depending on the% of use you may be different. This example being the same is a coincidence. In programming coincidences do not count, so do not count on them, this context is different from others, you are looking at the photo and not the movie.

It is not recommended to use ALL_CASE notation at all in C ++.

    
12.11.2018 / 12:18