-
Junior Member
Find the size of a datatype
Is there any way to find the size of a datatype without using "sizeof()" operator?
-
Contributing Member
Re: Find the size of a datatype
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
-
Junior Member
Re: Find the size of a datatype
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
-
Junior Member
Re: Find the size of a datatype
entadi asalu artham kavatam ledu
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules