calculating size of int without sizeof operator

Go4Expert Member
12Feb2008,19:59   #1
wrecker's Avatar
is it possible to calculate the size of int, float, char and other data types without using sizeof operator.
one method i thought was using the << operator but it isnt working..
Light Poster
12Feb2008,20:16   #2
johnson.reddy's Avatar
Code:
#include <stdio.h>

#define FAIRLY_LARGE 32767

int main(void)
{
char buf[FAIRLY_LARGE] = {0};
puts("Please enter the size of an int, in bytes.");
if(fgets(buf, FAIRLY_LARGE, stdin) != NULL)
{
~fputs("The size of an int, in bytes, is ", stdout);
puts(buf);
}
return 0;
}

Last edited by shabbir; 12Feb2008 at 21:03.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
Light Poster
12Feb2008,20:17   #3
johnson.reddy's Avatar
1.
#include<iostream>
2.

3.
#define _sizeoftype(T) ((int)(((T*)0)+1))
4.

5.
template <typename T, size_t N >
6.
size_t arrayLength(const T (&)[ N ],size_t sizeoftype ) {
7.
return sizeoftype*N;
8.
}
9.

10.
int main() {
11.
int array[7];
12.
std::cout<<"SizeOf Array :"<<arrayLength(array,_sizeoftype(int))<<std::endl ;
13.
return 0;
14.
}
15.
Light Poster
12Feb2008,20:18   #4
johnson.reddy's Avatar
sorry for not using " rule for writing code"
Nest time i will use .
Light Poster
12Feb2008,20:20   #5
johnson.reddy's Avatar
Code:
double i;

double * p = &i;

double * q= p;

p++;

cout<<(int)p-(int)q<<endl;
Light Poster
12Feb2008,20:22   #6
johnson.reddy's Avatar
what i am trying to say you atleast you have search on google. you will get easily.
TechCake
12Feb2008,20:31   #7
asadullah.ansari's Avatar
Code:
int main() 
{ 
int var = 01; 
int cnt = 0; 
int siz; 
while(var) 
{ 
  var << =  1; 
  cnt++; 
} 
siz = cnt/8; 
printf("size of integer %dn", siz);
return 0;
}

Last edited by shabbir; 12Feb2008 at 21:03.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
TechCake
12Feb2008,20:31   #8
asadullah.ansari's Avatar
Code:
int main() 
{ 
int var = 01; 
int cnt = 0; 
int siz; 
while(var) 
{ 
  var << =  1; 
  cnt++; 
} 
siz = cnt/8; 
printf("size of integer %dn", siz);
return 0;
}
TechCake
12Feb2008,20:32   #9
asadullah.ansari's Avatar
try many ways to find. Just try with pointers , differences and make generic for it.
Go4Expert Founder
12Feb2008,21:04   #10
shabbir's Avatar
Guys please use code blocks when you have code snippets in the posts.