calculating size of int without sizeof operator

wrecker's Avatar, Join Date: Mar 2007
Go4Expert Member
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..
0
johnson.reddy's Avatar
Light Poster
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
0
johnson.reddy's Avatar
Light Poster
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.
0
johnson.reddy's Avatar
Light Poster
sorry for not using " rule for writing code"
Nest time i will use .
0
johnson.reddy's Avatar
Light Poster
Code:
double i;

double * p = &i;

double * q= p;

p++;

cout<<(int)p-(int)q<<endl;
0
johnson.reddy's Avatar
Light Poster
what i am trying to say you atleast you have search on google. you will get easily.
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
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
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
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;
}
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
try many ways to find. Just try with pointers , differences and make generic for it.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Guys please use code blocks when you have code snippets in the posts.