1

I've been getting a lot of criticism for using uint instead of size_t, but every time I check the toolchain I am working with turns out size_t is defined as a uint.

Are there any compiler implementations where size_t is actually not a uint? What are the grounds for that criticism?

  • 1
    What is a "uint"? Assuming you mean `unsigned int`: http://coliru.stacked-crooked.com/view?id=b36d2ed363b5e1661e4a1a57d3a0e554-76238ab73c2fc168e6d731d9f34df389 – R. Martinho Fernandes Jun 11 '13 at 09:41
  • You never know what the future is made of. `size_t` is standard, elegant and meanfull. Better using it :) – Thomas Ruiz Jun 11 '13 at 09:42
  • size_t make your code protable for every flatforms of computer will define their own size_t. – MIKU_LINK Jun 11 '13 at 09:43
  • 2
    Compile your program with 64-bit compiler. You may get some warnings over this. – doptimusprime Jun 11 '13 at 09:47
  • `size_t` is standard, `uint` is non-standard. Also on 64 bit architectures `size_t` will be 64 bit (unsigned) whereas `uint` could easily be 32 bit. – Paul R Jun 11 '13 at 09:50

1 Answers1

6

size_t is the "size matching the largest possible address range you can use in the machine" (or some words to roughly that effect).

In particular, size_t will be 64 bits on a 64-bit machine, and 32 bits on a 32-bit system.

I'm assuming uint is short of unsigned int, which is pretty much universally 32 bits (these days, some older systems would be using 16-bit integers). So on a 64-bit system, an unsigned int will be 32 bits still, although memory allocations, strings, etc can be larger than 32 bits in size - which would cause problems if you are trying to use uint for the size.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • IIRC it's also required to be unsigned – Balog Pal Jun 11 '13 at 09:54
  • The type for size_t is not guaranteed to be unsigned, but it's guaranteed to be "large enough for any memory allocation in the system". So it will never overflow (unless you use it "badly"). – Mats Petersson Jun 11 '13 at 09:55
  • @MatsPetersson - don't have a 64 bit compiler right now, but I assume an `int` to be the CPU register width, and logically, and logically same for `uint`. Is that not the case? –  Jun 11 '13 at 09:58
  • 2
    @user2341104: while original intention of int was indeed to match natural register size, approaching 64 bit many ABIs decided it would be terrible waste and made it 32-bit even for 64 bit registered platforms – Balog Pal Jun 11 '13 at 09:59
  • No, `int` is definitely not 64-bit on a typical 64-bit system (Windows, Linux, MacOS all use 32-bit `int`, and aside from Windows, `long` is 64 bit - Windows by tradition has a load of code that uses `long` in places that make more sense to be 32 bit, so they decided that `long long` is required for 64-bit values). – Mats Petersson Jun 11 '13 at 10:01