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

Asked

Viewed 485 times

11

A few days ago studying about emulation I came across a question in the English stack overflow about CHIP8 emulation, and one of the answers said not to use C arrays as for example int i[12] instead use std::array for being a fixed type, it left me a little confused about what would be a fixed type, another example would be a type of byte that normally people create with typedef unsigned char BYTE, in C++ has the equivalent std::uint8_t only that opening his definition in 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?

  • 2

    In relation to std::array he is merely a wrapper over a classic C com array that allows you to easily move to functions and continue to use C++ idiomatic things like foreach without losing information about the size of the array. It ends up being easy enough, without any significant disadvantage.

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 list is not exhaustive):

  • Bit size remains the same in any environment. You know a whole 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, your only guarantee is that sizeof(char) == 1, and that sizeof(char) <= sizeof(short), nothing else. There is information about the bit size of these types, and they can vary from environment to environment. This is the number one problem that fixed types solve. The implementation of these types can take advantage of normal types, as you have already noted. Implementation does not matter, what matters are the guarantees 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 will not provide them (in practice it is unlikely today).
  • Everybody knows these guys and knows their shit. Since these fixed types are in the standardization of C++, it is assumed that the programmer knows their use cases, implications and behaviors. If the programmer defines a new type, another programmer will not know of its existence, so you need to learn about all the points I listed. This creates unnecessary friction, because already known types already exist in the standardization precisely because of this.
  • Specific problems need specific types. The guy std::size_t is used to store all possible values of sizeof in an environment. The type std::ptrdiff_t is the resulting type of the result of subtracting two pointers, and is used for pointer arithmetic and for sorting indexing (Indexing array). The guy 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 type of largest bit size. The type std::int32_t ensures to be 32 bits in size, has no padding and uses 2’s Complement for negative numbers. Finally, each type of this has its implications, use cases etc.

You can see all kinds of fixed size here: https://en.cppreference.com/w/cpp/header/cstdint. Some other guys are here: https://en.cppreference.com/w/cpp/header/cstddef

6

Without seeing the context it gets a little complicated to answer, including because it is common the person who is explaining what he read has misinterpreted.

I believe you’re just talking about a guy array with fixed size, as is the array of C. It would be an opposition to a array which can be expanded if necessary. You can see more on Difference between Std::list, Std::vector and Std:array.

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

The question is about the types that have the guaranteed bit quantity. 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 it would generate the code, which could give better efficiency. Over time it was found that in many cases it would be interesting to have types where you know the exact size to be used. C and C++ adopted this, but the pattern still remained the exact size type not specified in advance. So these guys 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 legacy. in this context it is a type nickname.

Normally type aliases are used for the sake of readability (show intention) and to give some flexibility, so you use an abstract type and depending on the include that using concrete can 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, is looking at the photo and not the film.

It is no longer recommended to use ALL_CASE notation for anything in C++.

  • My question arose when I read this answer: https://codereview.stackexchange.com/a/148802

  • 1

    @Samuelives with context saw that the question has nothing to do with what you asked. Which is even weirder because I’ve accepted the other question that the focus is on the part that has nothing to do with what you actually want to know. I need to think if it’s not the case to close as unclear.

  • The question was the curiosity I had when reading this answer even more when I saw this quote: Use Fixed-width integer types (in particular Std::uint16_t Instead of unsigned short and Std::uint8_t Instead of unsigned char), because in the definition it is exactly the same

6

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

As to the typedef, it just creates a "nickname" for the type, but in the end it is 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++ 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.