I'm using C++11 and higher and I was asking myself if it is safe to get the number of bytes required for a datatype like std::uint16_t
to send over length agnostic protocols. More precise is it safe to call sizeof(std::uint16_t)
and can I assume that this is always 2? What about std::int16_t
?
Asked
Active
Viewed 619 times
2
-
4That's exactly what `sizeof` is for, why wouldn't it be safe? – Barmar Jul 30 '20 at 17:32
-
1@Barmar Because see eerorika’s answer. – Konrad Rudolph Jul 30 '20 at 17:37
-
1better yet, `static_assert(CHAR_BIT == 8);` and forget about it, since it's unlikely that your code is ever going to run on a machine that has a different size of byte. – Aykhan Hagverdili Jul 30 '20 at 18:05
1 Answers
6
Safe way to get number of bytes for a data type in C++
That would be the sizeof
operator.
More precise is it safe to call sizeof(std::uint16_t) and can I assume that this is always 2?
No. You can only rely on that when byte is 8 bits.
What about std::int16_t?
Same.
For network communication, what you may need to know is how many octets a type is. That can safely be calculated like this: sizeof(std::uint16_t) * (CHAR_BIT / 8)
, and will be 2 for std::uint16_t
. Note that not all systems necessarily have the std::uint16_t
type.

eerorika
- 232,697
- 12
- 197
- 326
-
-
2@jjj No. A byte is `CHAR_BIT` bits by definition. There are CPU's with different sized bytes. – eerorika Jul 30 '20 at 17:35
-
[C standard regarding sizeof overflowing size_t](https://stackoverflow.com/q/57764670/10147399) – Aykhan Hagverdili Jul 30 '20 at 17:37
-
@eerorika The link is about `sizeof(SomeType)` overflowing on its own – Aykhan Hagverdili Jul 30 '20 at 17:41
-
@Ayxan In C++, `size_t` is specified to be large enough to contain the size any object. `sizeof` cannot overflow `size_t`, because no object exists that would overflow it. – eerorika Jul 30 '20 at 17:44
-
2@Ayxan That can't happen in C++: https://timsong-cpp.github.io/cppwp/support.types.layout#3 – NathanOliver Jul 30 '20 at 17:45
-
For network communication it is likely that both devices use 8-bit bytes, as far as I know only microcontrollers have oddly sized bytes. It is not guaranteed however, that both ends use the same endianness, that is a topic that needs to be addressed when exchanging data between different devices. – Havenard Jul 30 '20 at 17:45
-
-
@NathanOliver that's not the standard, is it? Which part of the standard says that? – Aykhan Hagverdili Jul 30 '20 at 17:46
-
@Ayxan That is a link to the current standard draft. That text has been in every C++ standard that I've looked at. – NathanOliver Jul 30 '20 at 17:46
-
I would love to see a real device which has a byte not equal to 8 bits. Yes, they are possible in theory, but I am yet to see one. – SergeyA Jul 30 '20 at 17:50
-
@NathanOliver Alright, maybe it's different for the C language which [the link](https://stackoverflow.com/q/57764670) is discussing. I myself have asked that question on SO previously [here](https://stackoverflow.com/q/57827073) and it was closed as a duplicate of the question I liked and a couple others. – Aykhan Hagverdili Jul 30 '20 at 17:51
-
@SergeyA get yourself one of these: https://www.ti.com/lit/ug/spru281f/spru281f.pdf?ts=1596131493493&ref_url=https%253A%252F%252Fnews.ycombinator.com%252F – eerorika Jul 30 '20 at 17:52
-
1@Ayxan C and C++ are different languages. This Q is tagged as C++, so the C answers might not be correct, and in this case, they aren't. – NathanOliver Jul 30 '20 at 17:52
-
@SergeyA Is [CHAR_BIT ever > 8?](https://stackoverflow.com/q/32091992/10147399) – Aykhan Hagverdili Jul 30 '20 at 17:52
-
-
1@SergeyA I believe the IBM/360 line of machines is such an example and others exist. – Jesper Juhl Jul 30 '20 at 17:57
-
@JesperJuhl We're raising some seriously old systems here though, I think the real question is, do we have current client/server systems with `CHAR_BIT` different to 8? I'm pretty sure that ever since the IBM PC took off this has become the standard, and we only really see different in those tiny ICs with no more than a couple bytes of memory. – Havenard Jul 30 '20 at 19:09
-
@Havenard Old systems are still out there and running things though. The product I work on still needs to work with a ~20 year old AS/400 system because it's still in use at a customer site. Old systems creep up all the time and are not as theoretical as some may think at first glance. – Jesper Juhl Jul 30 '20 at 19:12
-
20 years isn't that old, virtually all the standards we use today have already been established then. – Havenard Jul 30 '20 at 19:14