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 ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    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?

    Thanks a lot!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    size_t is the same as unsigned int (simple answer, but not technically accurate).

    A wide char (i.e. wchar_t) is usually an unsigned short. It's used for UNICODE text to represent languages other than English which don't easily fit into a single byte (Japanese for example).

    There's a long double, but from what I read in the MSDN, VC++ treats it the same as a double.
    Last edited by cpjust; 04-11-2008 at 07:39 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks for the replies.

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

    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?

    Thanks again.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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).

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    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:
    Code:
    #include <stdio.h>
    #include <stdint.h>
    
    static void f(size_t s)
    {
      printf("&#37;zu\n", s);
    }
    
    int main(void)
    {
      f(1);              /* int; Fits into size_t. */
      f(-1);             /* int; Doesn't fit; defined behavior. */
      f(1.1);            /* double; Fits when .1 is discarded. */
      f(SIZE_MAX + 1.0); /* double; Doesn't fit after discarding, undefined behavior. */
    
      return 0;
    }
    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.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21