Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between __int32 vs int vs int32_t (again)

Tags:

c++

The answers around the web (one, two) only tell parts of the story, and omit some details that I would love you to help me to clarify:

  • int, by the C++ standard, must be >= 32 bits.
  • Different compilers (implementations) can make int to allocate different amount of bits by default. So Compiler A can make int to allocate 50 bits (for example) when I declare integer.
  • Also, different compilers may / may not use bit padding for optimization
  • __int32 (Microsoft-specifc) and int32_t are here to solve the problem and:
    • Force compiler to allocate exactly 32 bits.
    • Never use bit padding before / after this data type.

Which one of my guesses are correct? Sorry to reasking old question, but I am actally confused in all of that allocation features.

Thanks in advance for your time.

like image 320
Petro Ivanenko Avatar asked Mar 08 '26 06:03

Petro Ivanenko


1 Answers

Which one of my guesses are correct?

  • int, by the C++ standard, must be >= 32 bits.

Not correct. int must be >= 16 bits. long must be >= 32 bits.

Different compilers (implementations) can make int to allocate different amount of bits

Correct.

... by default.

I don't know of a compiler with configurable int sizes - it usually depends directly on target architecture - but I suppose that would be a possibility.

Also, different compilers may / may not use bit padding

They may. They aren't required to.

__int32 (Microsoft-specifc) and int32_t are here to solve the problem and:

  • Force compiler to allocate exactly 32 bits.

  • Never use bit padding before / after this data type.

Correct. More specifically, std::int32_t is an alias of one of the fundamental types which has exactly 32 bits and no padding. If such integer type is not provided by the compiler, then std::int32_t alias will not be provided either.

Microsoft documentation promises that __int32 exists and that it is another name of int, and that it has 32 non-padding bits. Elsewhere, Microsoft documents that int32_t is also an alias of int. As such, there is no difference other than __int32 not being a standard name.

like image 200
eerorika Avatar answered Mar 10 '26 19:03

eerorika