The size_t confusion

Apparently there could be a lot of confusion with regards to size_t. In 'C' the size_t was defined as an unsigned integer. 'C++' defines it as unsigned integer as well but it has a namespace of std around it. Some compilers may want #include<cstdlib> to use std::size_t.

In any case, it can cause quite some confusion among programmers as to why it is used and what is the size of it. Instead we can #define a type, give it a meaningful name and use it accordingly. Here is my simple example:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>
#include<cstdlib> //Used for std::size_t ... on some compilers

#define u16 unsigned short
#define u32 unsigned //integer is implicit

int
main()
{

size_t size = sizeof(size_t);
std::cout << "Size of size_t is " << size << std::endl;

std::size_t size2 = sizeof(std::size_t);
std::cout << "Size of std::size_t is " << size2 << std::endl;

size_t size3 = sizeof(u16);
std::cout << "Size of u16 is " << size3 << std::endl;

size_t size4 = sizeof(u32);
std::cout << "Size of u32 is " << size4 << std::endl;

return
0;
}





The output is as follows:

Check out this stream