Understanding the size_t datatype in C / C++

Filed Under: C++
Size T C Cpp

In this article, we’ll be taking a look at understanding the size_t datatype in C/C++.

When you encounter the function prototype of a lot of C/C++ library functions, you may be wondering what exactly this stands for.

Let’s understand what it means, using some illustrative examples!


The size_t datatype in C/C++

Essentially, size_t is an unsigned datatype. This means that it can’t be negative.

size_t value; // value will never be negative

It can store the maximum size of any datatype. So this is suitable for use on 64-bit systems, when you may face an issue with 32-bit integers via unsigned int.

When you’re using int for counting anything, there is a chance that your value may overflow and be negative!

Take the following example, wherin our integer variable suddenly becomes < 0, due to overflow!

#include <stdio.h>

int main() {
    // Initially set to 0
    int count = 0;

    while (count >= 0) {
        count ++;
        if (count < 0) {
            printf("Overflow Detected! Count is now %d\n", count);
        }
    }
    return 0;
}

You may need to wait for a few seconds to see the output.

Output

Overflow Detected! Count is now -2147483648

As you can observe, our count integer variable is negative, due to an overflow!

While one way is to place a check on our integer variable, it is often very convenient to use an unsigned datatype which would always be non-negative.

The C standard puts size_t as one such datatype. This will guarantee that whatever we are counting will not be negative. This is very useful if you are keeping track of an array index, for example.

This is why functions like strlen() return a size_t value.


Caveats of using size_t

While this is a good datatype to ensure that our variable stays non-negative, it may not work if you want to deal with subtraction.

For example, consider the below sample of code, which tries to find the difference of two size_t variables.

#include <stdio.h>

int main() {
    size_t a = 1000;
    size_t b = 5000;

    // Observe the output!
    printf("Difference between %lu and %lu = %lu", a, b, a - b);

    return 0;
}

On running the above code, you may get something similar to this:

Output

Difference between 1000 and 5000 = 18446744073709547616

Why does this happen?!

Since the subtraction between two signed numbers is different compared to that of two signed integers, we get unexpected output.

In this case, the best solution would be to make them integers and then compute the difference.

Similar to overflowing of ints, we have a similar case for size_t too.

So make sure you choose the datatype of your variable based on the problem to solve.


Conclusion

In this article, we learned how we could use the size_t datatype in C / C++.

For similar content, do go through our tutorial section on C programming.


References


Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages