meaning of size_t

I recently came across this keyword size_t,I read about it in the official C++ documentation but didn't quite able to get my head around it. I don't have much experience or profound knowledge in C++. Can someone kindly explain it to me in simple terms. I would be very grateful to him/her. Looking forward to get your replies.

Thanks
std::size_t is a type alias for a suitable unsigned integer type, (like unsigned long long,) which is large enough to represent the size of (number of bytes in) the largest array possible.

std::size_t is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.
http://en.cppreference.com/w/cpp/types/size_t
Last edited on
why is that it's used for array indexing and loop counting??? Is there any sort of benefit that we get out of it or it's just the traditional which asks us to follow ???
> why is that it's used for array indexing and loop counting??? Is there any sort of benefit that we get

It makes our code portable across different implementations.
For example:
1
2
3
4
void increment_array( int array[], std::size_t num_elements )
{
      for( std::size_t i = 0 ; i < num_elements ; ++i ) ++array[i] ;
}
Thanks so much. I didn't know that at all. Can you please point further what it means when you said "is a type alias for a suitable unsigned integer type"?? Please provide it with some examples,that would help me to understand it in much better way. And thanks again :)
A type alias is another name for a type.

For example, with:
1
2
3
4
using my_int_type = long ; // 'my_int_type' is another name for the type 'long'
typedef long int_type ; // alternative syntax

my_int_type n = 0 ; // type of 'n' is long 


http://en.cppreference.com/w/cpp/language/type_alias
http://en.cppreference.com/w/cpp/language/typedef
Thank u so much,I got it now :)
Topic archived. No new replies allowed.