29

I'm quite new to c++, but I've got the hang of the fundamentals. I've come across the use of "Uint32" (in various capitalizations) and similar data types when reading other's code, but I can't find any documentation mentioning them. I understand that "Uint32" is an unsigned int with 32 bits, but my compiler doesn't. I'm using visual c++ express, and it doesn't recognize any form of it from what I can tell.

Is there some compilers that reads those data types by default, or have these programmers declared them themselves as classes or #define constants?

I can see a point in using them to know exactly how long your integer will be, since the normal declaration seems to vary depending on the system. Is there any other pros or cons using them?

Zoomulator
  • 20,774
  • 7
  • 28
  • 32

6 Answers6

19

Unix platforms define these types in stdint.h, this is the preferred method of ensuring type sizing when writing portable code.

Microsoft's platforms do not define this header, which is a problem when going cross-platform. If you're not using Boost Integer Library already, I recommend getting Paul Hsieh's portable stdint.h implementation of this header for use on Microsoft platforms.

Update: Visual Studio 2010 and later do define this header.

oz10
  • 153,307
  • 27
  • 93
  • 128
13

The C99 header file stdint.h defines typedefs of this nature of the form uint32_t. As far as I know, standard C++ doesn't provide a cstdint version of this with the symbols in namespace std, but some compilers may, and you will typically be able to include the C99 header from C++ code anyways. The next version of C++ will provide the cstdint header.

You will often see code from other people who use non-standard forms of this theme, such as Uint32_t or Uint32 or uint32 etc. They typically just provide a single header that defines these types within the project. Probably this code was originally developed a long time ago, and they never bothered to sed replace the definitions out when C99 compilers became common.

Akseli Palén
  • 27,244
  • 10
  • 65
  • 75
Greg Rogers
  • 35,641
  • 17
  • 67
  • 94
12

Visual c++ doesn't support the fixed-width integer types, because it doesn't include support for C99. Check out the answers to my question on this subject for various options you have for using them.

Community
  • 1
  • 1
Dani van der Meer
  • 6,169
  • 3
  • 26
  • 45
  • 3
    Very nice. I think I'll be using the boost library for it since it's crossplatform. Damn microsoft and their do-it-their-own-way principles. – Zoomulator May 26 '09 at 14:53
  • 3
    Microsoft isn't really to blame here: they adhere strictly to the standard: C++ simply hasn't got any standardized fixed-size number types yet. – Konrad Rudolph May 26 '09 at 15:21
  • 1
    I use the boost types too. Microsoft will eventually support these types because they will be part of the C++ standard. – Dani van der Meer May 26 '09 at 15:22
  • 2
    Well, I do blame Microsoft for not supporting C99. There is stuff in there that I want. Most of it is already implemented in Microsoft-proprietary ways, but some of it isn't, and in any event all the `#ifdef` statements make my code bigger than necessary. – steveha May 05 '12 at 00:43
3

The main reason for using them is that you then don't have to worry about any possible problems arising when switching between 64bit and 32bit OS.

Also if you are interfacing to any legacy code that you new was destined for 32bit or even 16bit then it avoids potential problems there as well.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • That's a good thing to keep in mind when working with cross platform projects I'm sure. – Zoomulator May 26 '09 at 14:56
  • They're also quite useful when implementing distributed applications where integers being sent over the wire between client and server must be of a specific size, as defined by their communication protocol. – Void - Othman May 26 '09 at 17:06
1

Try UINT32 for Microsoft.

The upper case makes it clear that this is defined as a macro. If you try to compile using a different compiler that doesn't already contain the macro, you can define it yourself and your code doesn't have to change.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

uint32 et al. are defined by macros. They solve a historic portability problem of there being few guarantees across platforms (back when there there more platform options than now) of how many bits you'd get when you asked for an int or a short. (One now-defunct C compile for the Mac provided 8-bit shorts!).

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46