Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I rely on sizeof(uint32_t) == 4?

Tags:

c++

I know I can rely on sizeof(char) == 1, but what about sizeof(uint32_t) and sizeof(uint8_t)? Shouldn't it be 32bit (8bit) in size guessing from the name? Thanks!

like image 816
tzippy Avatar asked Jul 24 '14 08:07

tzippy


People also ask

What does uint32_t mean in C?

uint32_t is a numeric type that guarantees 32 bits. The value is unsigned, meaning that the range of values goes from 0 to 232 - 1.

How many bits is uint32_t?

UInt32: This Struct is used to represents 32-bit unsigned integer.


2 Answers

Absolutely not, at least with regards to sizeof(uint32_t). sizeof returns the number of bytes, not the number of bits, and if a byte is 16 bits on the platform, sizeof(uint32_t) will be 2, not 4; if a byte is 32 bits (and such platforms actually exist), sizeof(uint32_t) will be 1 (and uint32_t could be a typedef to unsigned char).

Of course, in such cases, uint8_t won't be defined.

like image 69
James Kanze Avatar answered Sep 23 '22 13:09

James Kanze


The fixed-sized types will always be exactly that size. If you're on some weird platform that doesn't have integer types of that size then they will be undefined.

Note that it doesn't necessarily follow that sizeof(uint32_t) == 4, if CHAR_BIT != 8; again this only occurs on weird platforms.

like image 33
ecatmur Avatar answered Sep 22 '22 13:09

ecatmur