What is size_t for? ...and some other variable type questions.

This is a discussion on What is size_t for? ...and some other variable type questions. within the C Programming forums, part of the General Programming Boards category; I've seen in many programs and in many function declarations the 'size_t' thingy. I've also searched in the internet, and ...

What is size_t for? ...and some other variable type questions.

I've seen in many programs and in many function declarations the 'size_t' thingy. I've also searched in the internet, and all I find is: "size_t is the unsigned integer result of the sizeof keyword" but no explanations. What exactly is it? I've seen in some programs the size_t used to declare variables, but I don't know what's the difference between size_t and any other variable type. Can somebody give me a simple example in which size_t works?

My other question: what is a wide char? Of this I really have no idea, I have not found any n00b info on the internet.

And last: Is there a bigger variable type than double? Is there any way to make bigger variable types to hold monumental (talking about 1*10^99) numbers?

A size_t is the C type for a size. (Not S, M, L, XL, and XXL, but sizes of structures and variables in memory.) It is often mapped to a "basic" type -- on my system, it's an unsigned long (if I'm reading my headers correctly).

A wide char is a character wider than one byte.

There is a long double; and for that matter double, in most places, goes up to 10^308. (You don't get all 308 digits exactly, of course.)

If a function expects size_t, then it accepts unsigned int. There are such things as integer conversions: char and short are promoted to int (a temporary int is created with the same value, or at least you can pretend that it is), and then that int is treated as unsigned (so -1 is suddenly four billion and change).

Ok so... size_t is exactly the same thing as unsigned int? There is nothing that great about it then?

No. size_t is an unsigned integral type, but it need not be unsigned int. It's entirely possible for size_t and unsigned int to be different sizes; 64- and 32-bit respectively, for example.

And what about a function that accepts a size_t variable? Would that mean that it accepts any type of variable (the value returned by sizeof) or does it mean that it only accepts unsigned ints?

That's a mildly complex question, because C can do all sorts of conversions between types, and the sizes of types are not mandated by the C standard. If a function takes a size_t argument, anything you pass will be converted to size_t, however that happens.

If you pass an integral value that happens to fit inside a size_t (that is, it's not negative and it's less than size_t's largest value {SIZE_MAX on C99}), then it will retain its value.

If you pass an integral value that is outside of the range of what size_t can hold, it will be modified to fit, using these rules as quoted from C99: "[T]he value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type."

If you pass a floating point value, the first thing that happens is that the fractional part is discarded. If the resulting (integral) value doesn't fit inside of a size_t, the behavior of your program is undefined (which you might guess is a bad thing). If the value does fit, then it's stored as-is.

This program sort of demonstrates what's going on. It assumes that SIZE_MAX + 1.0 is a valid double value, which is a pretty reasonable assumption. This requires a C99 implementation:

I ran this with three compilers: gcc, Intel's C compiler, and Sun Studio 12. They all agreed on the first three outputs: 1, 4294967295, 1. All compilers will output the values 1, SIZE_MAX, and 1 for these (pretending for a moment that I don't have that undefined behavior in the last one; because of it, anything goes). However, the compilers differ on the last value. Intel and Sun output 0; gcc outputs 4294967295. Both of these are "correct" because the behavior is undefined; they can print (or do!) anything they please.

Last edited by cas; 04-10-2008 at 08:24 PM.
Reason: Minor cleanup of a potential ambiguity.

Cas is right in this case that size_t is not unsigned int. According to the standard, it's the value returned by some functions, but it has no absolute requirements on what type or how big it is. Just that it might be unsigned.

Also note that cas's example may compile, but the compiler may also throw a warning about truncation of the type you pass if it does not fit properly within a size_t.