Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is int8_t if a machine has > 8 bits per byte?

Tags:

c++

c++11

I was reading the C++ FAQ and it says

The C++ language guarantees a byte must always have at least 8 bits

So what does that mean for the <cstdint> types?

Side question - if I want an array of bytes should I use int8_t or char and why?

like image 222
David Avatar asked Dec 14 '11 18:12

David


People also ask

What type is int8_t?

Thus, int8_t denotes a signed integer type with a width of exactly 8 bits.

Is uint8 always 1 byte?

In C, the unsigned 8-bit integer type is called uint8_t . It is defined in the header stdint. h . Its width is guaranteed to be exactly 8 bits; thus, its size is 1 byte.


2 Answers

C++ (and C as well) defines intX_t (i.e. the exact width integer types) typedefs as optional. So, it just won't be there if there is no addressable unit that's exactly 8-bit wide.

If you want an array of bytes, you should use char, as sizeof char (and signed char and unsigned char) is well-defined to always be 1 byte.

like image 124
Cat Plus Plus Avatar answered Sep 20 '22 19:09

Cat Plus Plus


To add to what Cat Plus Plus has already said (that the type is optional), you can test whether it is present by using something like:

#ifdef INT8_MAX
//  type int8_t exists.
#endif

or more likely:

#ifndef INT8_MAX
#error Machines with bytes that don't have 8 bits aren't supported
#endif
like image 38
James Kanze Avatar answered Sep 18 '22 19:09

James Kanze