Is there any way to find the size of a datatype without using "sizeof()" operator?
Printable View
Is there any way to find the size of a datatype without using "sizeof()" operator?
Why to go for another choice if we have sizeof operator??
Anyways,we can find out like this:
#define sizeof_type( type ) (size_t)((type*)1000 + 1 )-(size_t)((type*1000)
------------
Neelima
Here is a full program as a solution
#include <iostream>
#include <conio.h>
using namespace std;
template <class T>
size_t getsize()
{
T *p;
T *q;
q=p++;
return reinterpret_cast<size_t>(p) - reinterpret_cast<size_t>(q);
}
int main()
{
cout<<sizeof(char)<<" "<<getsize<char>()<<endl;
cout<<sizeof(short)<<" "<<getsize<short>()<<endl;
cout<<sizeof(int)<<" "<<getsize<int>()<<endl;
cout<<sizeof(long)<<" "<<getsize<long>()<<endl;
getchar();
}
-Sandeep
entadi asalu artham kavatam ledu