Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   calculating size of int without sizeof operator (http://www.go4expert.com/forums/calculating-size-int-sizeof-operator-t8786/)

wrecker 12Feb2008 19:59

calculating size of int without sizeof operator
 
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..

johnson.reddy 12Feb2008 20:16

Re: calculating size of int without sizeof operator
 
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;
}


johnson.reddy 12Feb2008 20:17

Re: calculating size of int without sizeof operator
 
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.

johnson.reddy 12Feb2008 20:18

Re: calculating size of int without sizeof operator
 
sorry for not using " rule for writing code"
Nest time i will use .

johnson.reddy 12Feb2008 20:20

Re: calculating size of int without sizeof operator
 
Code:

double i;

double * p = &i;

double * q= p;

p++;

cout<<(int)p-(int)q<<endl;


johnson.reddy 12Feb2008 20:22

Re: calculating size of int without sizeof operator
 
what i am trying to say you atleast you have search on google. you will get easily.

asadullah.ansari 12Feb2008 20:31

Re: calculating size of int without sizeof operator
 
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;
}


asadullah.ansari 12Feb2008 20:31

Re: calculating size of int without sizeof operator
 
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;
}


asadullah.ansari 12Feb2008 20:32

Re: calculating size of int without sizeof operator
 
try many ways to find. Just try with pointers , differences and make generic for it.

shabbir 12Feb2008 21:04

Re: calculating size of int without sizeof operator
 
Guys please use code blocks when you have code snippets in the posts.


All times are GMT +5.5. The time now is 21:15.