Thread: when to use size_t ?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    when to use size_t ?

    When is it really used ? Can I use it as a substitute to int ? For eg. I have a datastructure :

    Code:
    struct mesh
    {
      int nvert; /*  Number of vertices */
      int ntri;  /* Number of triangles */
      vector *vert; /* List of vertices, size is nvert */
      triangle *tri; /* List of triangles,  size is ntri */
    };
    But the problem here is that nvert and ntri should be unsigned and I want to set an upper limit of may be a billion on both of them. Is it permissible to use size_t in this case ? Or should I use unsigned long. what is format specifier for size_t. Is it %lu ?

    Do you usually check the values entered by the user for ints, floats etc and compare it to their range/limit ? Or is it just over doing the error handling bit.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It's used when you need to deal with sizes in terms of memory, usually for calls like strlen() and malloc().

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You just really need to use it when you want to take the return from a function that returns size_t such as strlen.
    Otherwise you may experience data loss.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    SO I think I use unsigned long int in that case.

Popular pages Recent additions subscribe to a feed