1

I am looking for a practical example where I must use uint32_t instead of unsigned int for a desktop application. Can you please provide C++ code and:

  1. Explain the real world scenario; I am really focusing on a practical example here. Nothing too theoretical;
  2. Precise the target architectures + compilers;
  3. Explain why it will work with uint32_t and (most probably) fail with unsigned int;

My main objective is to be able to easily reproduce it if possible.

2
  • 1
    "Gimme code" questions rarely get very many answers (and the few they get tend to be quite poor). Commented Feb 7, 2014 at 7:14
  • I am really more asking for a practical example. "Gimme code" is just for me to reproduce and see by myself. Commented Feb 7, 2014 at 7:28

2 Answers 2

5

unsigned int may not be 32 bits, it's depends on the computer architecture your program runs on.

uint32_t is defined in library, and typedef as 32 bits integer.

The practice is more for portability. Even the target platform doesn't have uint32_t defined, you can easily typedef it to a 32 bits integer type.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you provide a real life example where we should use uint32_t instead of unsigned int? Also uint32_t is optional so it is never guaranteed to be there...
According to page en.wikipedia.org/wiki/64-bit, for now, most Unix and Unix-like system and MS Windows defined int as 32 bits. But HAL Computer Systems port of Solaris to SPARC64 defined int as 64 bits. So If you want to port program to this particular system, you will see issues.
@Korchkidu: If uint32_t is not there, you get a compiler error, define it, and move on. If unsigned int is not 4 bytes, you get a wrong executable. Compile time errors are infinitely preferable to corrupted data.
It is for portability when interacting in binary with external programs right?
5

Imagine that you wrote a portable program (i.e. it can be compiled and run on various different kinds of computers), and your program needs to be able to save and load a data file.

For simplicity, we'll say that your program's data file only needs to contain a single integer. So when the user clicks "Save", your program fwrite()'s an unsigned-int into the file. Since you're running on a modern Intel machine in 64-bit mode, the resulting file is eight bytes long (because sizeof(unsigned int)==8 on that platform).

Now you email the data file to your friend for him to use. Your friend loads in the file, but he compiled and ran your program on his old Pentium Pro machine. When he runs your program, it tries to read in a single int, as expected, but on a Pentium Pro, sizeof(unsigned int)==4, so the program only reads in 4 bytes rather than all 8. Now his program is displaying incorrect data, because it only read half of the file. That's no good.

If, on the other hand, you had specified in your save/load code to write/read a uint32_t rather than an unsigned int, you could rest assured that the file would always be 4 bytes long, no matter what architecture the program is compiled for. So by using uint32_t instead of unsigned int you made your data file easier to write and read correctly.

(Note that in real life you'd also want to use htonl() and ntohl() so that your data file would be the same for both Big Endian and Little Endian machines, but that's a separate issue)

1 Comment

Just to point out that for now, sizeof(unsigned int) for most of current system is still 4 bytes even on 64 bit hardware. But it may change in the future. See 64-bit data models section from en.wikipedia.org/wiki/64-bit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.