size_t

This is a discussion on size_t within the C Programming forums, part of the General Programming Boards category; Is size_t the same as an unsigned int? Thanks....

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    size_t

    Is size_t the same as an unsigned int? Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It might be (the same size). It doesn't have to be.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    It is an implementation defined value that typically is an unsigned word on the machine, but it is at the compiler writer's whim (well, probably not, but close enough) as to what the data type is.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    FOX
    Join Date
    May 2005
    Posts
    188
    What about situations like these? Should you declare i as size_t instead of int or unsigned int?
    Code:
    char *s = "Some string";
    int i; /* Should you use size_t i here? */
    size_t len = strlen(s);
    
    for (i=0; i<len; i++)
    {
            ; /* Do something */
    }

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    To be absolutely safe you should always follow the documentation for return types. You can get some real subtle bugs (even ones that can allow crackers to take over your program) if you use the wrong type.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > int i; /* Should you use size_t i here? */
    Yes.

    If you'd done
    int len = strlen(s);
    You'd have the conversion on the assignment instead of the comparison.
    The problem is still there, you just moved it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    FOX
    Join Date
    May 2005
    Posts
    188
    > It is an implementation defined value that typically is an unsigned word on the machine, but it is at the compiler writer's whim (well, probably not, but close enough) as to what the data type is.

    Is size_t guaranteed to be unsigned? I've read in several places that the ISO C standard says it must be unsigned, but I just wanted to make sure. I've seen lots of code that uses (size_t)(-1) to get the max value, and if size_t isn't unsigned then well...

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is size_t guaranteed to be unsigned?
    Yes. The standard says that size_t is an "unsigned integer type".

    >I've seen lots of code that uses (size_t)(-1) to get the max value, and if size_t isn't unsigned then well...
    There's a difference between -1 and -1 cast to size_t. Do a little test that prints -1 as a signed value, then as an unsigned value.
    My best code is written with the delete key.

  9. #9
    FOX
    Join Date
    May 2005
    Posts
    188
    > There's a difference between -1 and -1 cast to size_t. Do a little test that prints -1 as a signed value, then as an unsigned value.

    I know, that's why I wrote "to get the max value".

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The max/min values of data types are in a header file, limits.h I think.

  11. #11
    FOX
    Join Date
    May 2005
    Posts
    188
    I don't think the max value of size_t is defined anywhere. But you can easily get it by doing this cast anyway (size_t)(-1).

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I don't think the max value of size_t is defined anywhere.
    C99 adds SIZE_MAX in the stdint.h header.
    My best code is written with the delete key.

  13. #13
    FOX
    Join Date
    May 2005
    Posts
    188
    Oh that's good to know.

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