# finding data sizes

• 11-13-2002
joegio13
finding data sizes
I am trying find the sizes for data types but i am not allowed to use the "sizeof()" method. here is what i need to do, but without the "sizeof()"

cout<<"Size of type char is "
<<sizeof(char);

thank you
• 11-13-2002
Salem
Must be a homework from a rather odd teacher to solve a rather pointless problem in an unusual way.

What else are you not allowed to use?

Code:

```unsigned char ch = ~0; int count = 0; while ( ch != 0 ) { count++ ; ch >>= 1; }```
• 11-15-2002
CodeJerk
What is ~0
Code:

`unsigned char = ~0`
What does the ~0 do ? I had thought that ~ (tilde) was to be used only for destructors.
• 11-15-2002
OneStiffRod
the ~ reverses the bits, so 10001010 tilded would be 01110101.

It's good since it can find the max value of a data type from the min if 00000000 is 0 then tilded it's 11111111 would be the max value, unsigned it would be 255.
• 11-15-2002
FillYourBrain
you could use pointer math...

char *c = 0;
c++;

int * i = 0;
i++;

long *l = 0;
l++;

double *d = 0;
d++;

cout << "sizeof a char is " << (int)c << endl;
cout << "sizeof an int is " << (int)i << endl;
cout << "sizeof a long is " << (int)l << endl;
cout << "sizeof a double is " << (int)d << endl;

aren't I clever?:D
• 11-15-2002
OneStiffRod
That done be the mostest intelligents answer I've been seen.
• 11-15-2002
Salem
Heh - two more methods :D

Code:

```#include <iostream> #include <cstdlib> using namespace std; // difference between adjacent members of an array #define T1(type) {  \     type d[2];      \     cout << "sizeof(" #type ") is " << (int)&d[1] - (int)&d[0] << endl; \ } // difference between adjacent members of a struct // assumes that a struct containing a single type is always packed #define T2(type) {  \     struct foo { type a; type b; };  \     cout << "sizeof(" #type ") is " << offsetof(foo,b) << endl; \ } int main ( ) {     T1(char);     T2(char);     T1(short);     T2(short);     T1(int);     T2(int);     T1(double);     T2(double);     return 0; }```
• 11-15-2002
FillYourBrain
I told you I be smart!
• 11-16-2002
ammar
Isn't the ~ the bitwise NOT?