I have a lot of problems understanding the concept of size_t and will really appreciate if anybody can help me understand the concept. I searched a lot on the net and went on a multiple forums and answers ranged from "it's an unsigned integer type" to the more bizarre "it is an unsigned data type which is the size of the microprocessors data-bus and helps in address arithmetic." Can somebody explain the concept simply enough? I read an article (http://www.embedded.com/electrical-engineer-community/industry-blog/4026076/1/Why-size-t-matters) on size_t and what I understood is that as integers have a fixed range and pointers also have a fixed range to hold the address, it cannot access variables beyond it's range i.e. if the size of the int is greater than the size of the pointer then there's a problem. I can't understand how size_t solves the problem and what should you use size_t for?
It's for portability. The last paragraph of the link you posted explains it quite well. Given their example (the "I16LP32 processor"), let's assume we want to get the length of a C-string:
unsigned int length; length = strlen(str);
That's fine until the length of the string is greater than 65536 (however uncommon, still possible). So you might say, let's always use an
unsigned long. That's ok in this example, since it matches the pointer size, but it may be wasteful in some case. For instance, if a 16-bit system used long to represent a 32-bit integer.
If I remember correctly, their example is similar to how Windows operates:
longare 32 bits and
long longis 64 bits on a 32-bit system.
intis 32 bits and
long longare 64 bits on a 64-bit system.
They seem to use
long as the pointer size, but this isn't part of the standard, so it couldn't be guaranteed across systems and compilers.
Hope that helps.
size_t is not terribly complicated, I think you are probably reading more into it than there is.
size_t is defined in the standard as "the unsigned integer type of the result of the sizeof operator"
And that is basically it, it is an unsigned integer type. It is the type of the result of the sizeof operator but is also used by many standard library functions that return a size, strlen for example.
You should use size_t whenever you are calling an operator or library function (or function you've written yourself) that returns size_t. This enables better portability in your program and makes it more type correct.
Does that mean size_t is the same as unsigned integer for IPXX processors? What does size_t do? Is it used to allign the pointer and the object to it's maximum value?
Consider this implementation of the standard library. size_t is defined as:
/* size_t is defined in multiple headers */ #ifndef _HAS_SIZET #define _HAS_SIZET typedef unsigned size_t; #endif
That's all it is! Don't read too much into it, size_t is nothing more than a typedef for one of the supported unsigned integer types. It's used for a huge number of things such as collection sizes, counters, indexes, etc... Pretty much all of the places an unsigned integer would work, size_t can be used when you don't so much care about the exact type.