when to use size_t ?

This is a discussion on when to use size_t ? within the C Programming forums, part of the General Programming Boards category; When is it really used ? Can I use it as a substitute to int ? For eg. I have ...

  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,211
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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

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