The simplest example is quite dated: on an old 16-bit int system with 64K of RAM, the int value can be from -32768 to +32767, but after:
char buf[40960];
the buf buffer is 40 kilobytes, so sizeof buf too large to fit int , and it needs unsigned int .
The same thing can happen today if you use a 32-bit int , but you allow programs to access more than 4 GB of RAM at a time, as is the case with the so-called "I32LP64" (32 bits int , 64-bit long and pointer). Here the size_t type will have the same range as the unsigned long .
torek
source share