Is char the only type with a set size?

In the C ++ standard:

sizeof(char) , sizeof(signed char) and sizeof(unsigned char) are 1 .

  • Are there other types in C ++ that have fixed sizeof ?

  • Does an empty structure meet this definition?

+5
source share
3 answers
  • No. As you say, sizeof(signed char) , sizeof(unsigned char) and sizeof(char) defined by the standard as 1. Note that char must be either signed or unsigned , but it is always considered a separate type, sizeof everything else depends on implementations with some restrictions (for example, sizeof(long) cannot be less than sizeof(int) .)

  • The C ++ standard requires that the sizeof empty class be an integral value greater than zero (otherwise pointer arithmetic will break badly).

+10
source

1) Are there any other types in C ++ with a fixed size?

There are arrays with a given size:

 sizeof(T[N]) == N * sizeof(T) 

therefore sizeof(char[42]) 42 .

2) Does the empty structure meet this definition?

An empty structure is neither char , signed char , nor unsigned char , so it does not match this definition. (BTW its sizeof cannot be 0 ).

+1
source

As for question 1, you also have types in stdint.h .

-1
source

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


All Articles