Reputation: 81
In the C++ standard:
sizeof(char)
,sizeof(signed char)
andsizeof(unsigned char)
are1
.
Are there any other types in C++ which have a fixed sizeof
?
Does the empty structure correspond to this definition?
Upvotes: 5
Views: 299
Reputation: 218108
1) Are there any other types in C++ which have a fixed sizeof?
There are arrays which have specified size:
sizeof(T[N]) == N * sizeof(T)
so sizeof(char[42])
is 42
.
2) Does the empty structure correspond to this definition?
Empty struct is neither char
, signed char
or unsigned char
, so it doesn't correspond to this definition. (BTW its sizeof
cannot be 0
).
Upvotes: 1
Reputation: 234835
No. As you say, sizeof(signed char)
, sizeof(unsigned char)
, and sizeof(char)
are defined by the standard to be 1. Note that char
must be either signed
or unsigned
but it is always considered to be a distinct type. The sizeof
anything else is down to the implementation subject to some constraints (e.g. sizeof(long)
cannot be less than sizeof(int)
.)
The C++ standard requires sizeof
an empty class to be an integral value greater than zero (otherwise pointer arithmetic would break horribly).
Upvotes: 10