Thread: what is size_t and why should we use it

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    what is size_t and why should we use it

    im working my way through c unleashed and he is talking about being careful about what arguments you pass and undefined behaviour and makes a throw away comment about int len = strlen(s); being wrong because it should be size_t from reading other peoples posts and other discussions i had gleaned that size_t was a kind of macro for int but machine specific ie 16, 32, or 64 bits where as int was generally assumed to be 32 unless on a 16 bit machine in which case int would obviously be 16 bits long.

    coop

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    size_t represents an integer of sufficient storage to handle the large values you may encounter from some sources of it's use. There is no universal definition of what size_t will be (it could be an long, for example, or it could be a very large integer type). In particular, though, if you happen to be targeting a platform with a small atomic size (like a 16 bit machine), size_t is sufficient to return meaningful quantities from library routines that deal with much larger numbers than the atomic integer of that platform. Something similar can happen on 32 bit machines where 64 bit values may be required.

    It may be implemented as a macro in some compilers, or as a typedef in other contexts.

    You usually expect a warning if int len = strlen( s ) would potentially truncate the size returned, especially if the int on your platform is small (like 16 bits). There may be no warnings if the platform's integer is large and the compiler assumes it (some compilers still define int as 32 bits on 64 bit platforms, for example).

    size_t is a way of expressing a type which "automatically" assumes the appropriate size in these various context leading to better portability of your code.

    Since the library functions use size_t as the type, it is appropriate to do so yourself.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    There is no universal definition of what size_t will be (it could be an long, for example, or it could be a very large integer type).
    You mean unsigned right? A size_t is an implementation defined unsigned type.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by jimblumberg View Post
    You mean unsigned right? A size_t is an implementation defined unsigned type.
    so size_t would never be a negative number?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    Yes. Of course, in the strange world of C, you can still assign a negative number to it, but it will be converted to a non-negative number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    214
    Yes, it is unsigned, but as laserlight points out there's a fine line as to what that really means.

    To the processor the signed and unsigned integers are no different and there's no real distinction. There is a sign bit, but it only has meaning if you look at it

    The use of unsigned for size_t indicates the intended use of the entire bits available to express a quantity, and sizes of negative results don't make much sense to the context.

    So yes, I should have said unsigned.

    On the other hand, I can't confirm this because I'm recalling code from 30+ years ago, but I could swear there were occasions in some compilers on either AIX or some UNIX flavor where size_t was used as the type of a return value from a file function from the library, and a negative value (-1 probably) indicated an error (which, of course, we interpreted by using an int instead of an unsigned type).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    Quote Originally Posted by Niccolo
    On the other hand, I can't confirm this because I'm recalling code from 30+ years ago, but I could swear there were occasions in some compilers on either AIX or some UNIX flavor where size_t was used as the type of a return value from a file function from the library, and a negative value (-1 probably) indicated an error (which, of course, we interpreted by using an int instead of an unsigned type).
    Well, you could still use an unsigned integer type, and as long as the type and negative value is consistent it would work similiarly to std::string::npos from C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,876
    It would be a very unusual situation, but imagine if you had a system that used 16bit integers and a 64bit bus
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by Click_here View Post
    It would be a very unusual situation, but imagine if you had a system that used 16bit integers and a 64bit bus
    sounds like something Compaq would do im sure it was them that their hardware didn't fit in a slandered pc you needed adapters to fit the hard drives and special adapters for the isa/pci slots for their interface cards

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,876
    "You'll have to solder on your own inductors"

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    677
    My two drops of knowledge to the discussion:

    As Niccolo said, size_t is big enough to accommodate the maximum, theoretical, size of an array (theoretical because it's not yet possible, on 64 bits systems, to create an array of 16 EiB (2⁶⁴ bytes)!). Since it is a quantity, makes sense it is unsigned.

    There is also the type ssize_t, with the same size as size_t, but with one bit less in precision and signed. Usually this is the type used to return negative values as error indicators when a size is expected, like read():
    Code:
    ssize_t read(int fildes, void *buf, size_t nbyte);
    As a rule of thumb, size_t has the same size of a pointer, due to it's usage as an offset with the [] operator.

    size_t size depends on the architecture and processor mode of operation. For example, on Intel platforms, the old MS-DOS has 16 bits "small model" pointers and even on "large model" the pointers had 2 components: Segment:Offset, where offset was, still, 16 bits long. So size_t is 16 bits long there (as well as int type)... On i386 mode (32 bits mode), size_t is 32 bits long because a pointer is 32 bits long... On x86-64, 64, for the same reason (this 32/64 sizes applies to other processors as well).

    size_t and ssize_t are standard types (defined in stddef.h) which exists to try to make C code more portable.

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    725
    Quote Originally Posted by flp1969 View Post
    size_t and ssize_t are standard types (defined in stddef.h) which exists to try to make C code more portable.
    size_t is standard C, but ssize_t exists only in the POSIX standards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size_t
    By uryenugurkem in forum C Programming
    Replies: 3
    Last Post: 02-08-2018, 01:28 AM
  2. When to use size_t?
    By MutantJohn in forum C Programming
    Replies: 13
    Last Post: 04-07-2015, 05:15 PM
  3. size_t
    By KemalT in forum C Programming
    Replies: 5
    Last Post: 02-09-2015, 04:02 PM
  4. size_t
    By bos1234 in forum C Programming
    Replies: 1
    Last Post: 03-04-2011, 05:43 AM
  5. size_t
    By falconetti in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 08:52 AM

Tags for this Thread