What is the downside of replacing size_t with unsigned long

c++, integer

Solution

What warnings? The most obvious one I can think of is for a "narrowing conversion", that is to say you're assigning `size_t` to `unsigned int`, and getting a warning that information might be lost.

The main downside of replacing `size_t` with `unsigned long` is that `unsigned long` is not guaranteed to be large enough to contain every possible value of `size_t`, and on Windows 64 it is not large enough. So you might find that you still have warnings.

The proper fix is that if you assign a `size_t` to a variable (or data member), you should make sure that variable has a type large enough to contain any value of `size_t`. That's what the warning is all about. So you should not switch to `unsigned long`, you should switch those variables to `size_t`.

Conversely, if you have a variable that doesn't need to be big enough to hold any size, just big enough for `unsigned int`, then don't use `size_t` for it in the first place.

Both types (`size_t` and `unsigned int`) have valid uses, so any approach that indiscriminately replaces all use of them by some other type must be wrong :-) Actually, you could replace everything with `size_t` or `uintmax_t` and for most programs that would be OK. The exceptions are where the code relies on using an unsigned type of the same size as `int`, or whatever, such that a larger type breaks the code.

Problem

The library I am working on need to be used on both 32 and 64 bit machines; I have lots of compiler warnings because on 64bit machines `unsigned int != size_t`. Is there any downside in replacing all `unsigned int`s and `size_t`s by 'unsigned long'? I appreciate it does not look very elegant, but, in out case, the memory is not too much of an issue... I am wondering if there is a possibility of any bugs/unwanted behaviour etc. created by such `replace all` operation (could you give examples)? Thanks.

Original source