calculating size of int without sizeof operator

Discussion in 'C++' started by wrecker, Feb 12, 2008.

  1. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    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..
     
  2. johnson.reddy

    johnson.reddy New Member

    Joined:
    Feb 5, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Feb 12, 2008
  3. johnson.reddy

    johnson.reddy New Member

    Joined:
    Feb 5, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. johnson.reddy

    johnson.reddy New Member

    Joined:
    Feb 5, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    sorry for not using " rule for writing code"
    Nest time i will use .
     
  5. johnson.reddy

    johnson.reddy New Member

    Joined:
    Feb 5, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    double i;
    
    double * p = &i;
    
    double * q= p;
    
    p++;
    
    cout<<(int)p-(int)q<<endl;
    
    
     
  6. johnson.reddy

    johnson.reddy New Member

    Joined:
    Feb 5, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    what i am trying to say you atleast you have search on google. you will get easily.
     
  7. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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 a moderator: Feb 12, 2008
  8. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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;
    }
    
     
  9. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    try many ways to find. Just try with pointers , differences and make generic for it.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Guys please use code blocks when you have code snippets in the posts.
     
  11. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    But this doesnot work with calculating the size of float and other data types.
     
  12. aperisic

    aperisic New Member

    Joined:
    Feb 11, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    All solutions compare the size to char

    The third is simplest and is applicable to any class or structure besides basic types. Other two are more complex but interesting on their own nevertheless.

    Code:
    // if you prefere size in bytes
    #define BYTE_SIZE 8 
    
    // if you prefere size in bits
    // #define BYTE_SIZE 1 
    
    int sizeChar()
    {
    	static int c = sizeChar();
    	if (c != 0) return c; // call it only first time
    
    	c = 1;
    	char t = (char)1;
    	while ( t <<= 1 ) c++;
    	c /= BYTE_SIZE;
    
    	return c;
    
    }
    
    template<typename T>
    int my_sizeof_3()
    {
    	int c = sizeChar();
    
    	T t[2];
    	char* a[2];
    	
    	a[0] = (char*)&t[0]; // convert to char
    	a[1] = (char*)&t[1]; // convert to char at the second element
    	
    	int n = 0;
    
    	while (a[1] != a[0]) // go back until first and second match
    	{
    		a[1]--; 
    		n++;
    	}
    
    	return n*c;
    }
    The first solution uses two elements one after another and then changes slowly the entire portion of memory to see when the first element does not change any more. It means we have started to change the bytes in the second element and there we can calculate the size.
    The second solution needs assumed maximum expected size and then it shrinks and changes memory byte by byte and examine at which moment our element would show any change. At that moment we are inside the memory that is occupied by our element and we can calculate the size.

    sizeChar is there only if we really want to have the size of char calculated first

    Both functions can give size in bytes or size in bits depending on BYTE_SIZE definition.


    For the size of larger structure like class and struct we need the solution number 3

    Code:
    // if you prefere size in bytes
    #define BYTE_SIZE 8 
    
    // if you prefere size in bits
    // #define BYTE_SIZE 1 
    
    int sizeChar()
    {
    	static int c = sizeChar();
    	if (c != 0) return c; // call it only first time
    
    	c = 1;
    	char t = (char)1;
    	while ( t <<= 1 ) c++;
    	c /= BYTE_SIZE;
    
    	return c;
    
    }
    
    template<typename T>
    int my_sizeof_1()
    {
    	int c = sizeChar();
    
    	T e[2];
    
    	e[0] = 0;
    	e[1] = 0;
    
    	char* str = (char*)e;
    
    	int n = 0;
    	T p;
    
    	do
    	{
    		p = e[0];
    		str[n++] = 1;
    	}
    	while(e[0] != p);
    
    	return (n-1)*c;
    }
    
    #define MAX_EXPECTED_SIZE 20
    
    template<typename T>
    int my_sizeof_2(int max_size = MAX_EXPECTED_SIZE)
    {
    	int c = sizeChar();
    
    	char* e;
    
    	while(1)
    	{
    		e = new char[max_size];
    		
    		for(int i = 0; i < max_size-1; i++ )
    			e[i] = 0;
    
    		e[max_size-1] = 1; // set only last element to 1
    
    		T* y = (T*)e; 	// convert to see if type value
    // would remain 0
    
    		if(*y != 0) 
    		{
    			delete e;
    			break;
    		}
    
    		delete [] e;
    		
    		max_size--;
    	}
    
    	return max_size*c;
    }
    Usage

    Code:
    struct Sample
    {
    	int a,b,c,d,e,f,g;
    };
    
    	int size = my_sizeof_1<int>();
    	size = my_sizeof_1<double>();
    	size = my_sizeof_1<char>();
    	size = my_sizeof_1<long int>();
    
    	size = my_sizeof_2<int>();
    	size = my_sizeof_2<double>();
    	size = my_sizeof_2<char>();
    	size = my_sizeof_2<long int>();
    
    	size = my_sizeof_3<int>();
    	size = my_sizeof_3<double>();
    	size = my_sizeof_3<char>();
    	size = my_sizeof_3<long int>();
    	size = my_sizeof_3<Sample>();
    
    
     
  13. aperisic

    aperisic New Member

    Joined:
    Feb 11, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Of course, this is the fastest (sizeChar and BYTE_DEFINE as above)

    Code:
    [COLOR=RoyalBlue]template[/COLOR]<[COLOR=RoyalBlue]typename [/COLOR] T>
    int my_sizeof_4()
    {
    	[COLOR=RoyalBlue]int [/COLOR] c = sizeChar();
    
    	T* t = NULL;
    
    	[COLOR=RoyalBlue]return [/COLOR] (([COLOR=RoyalBlue]char[/COLOR]*)(t+1)-([COLOR=RoyalBlue]char[/COLOR]*)t)*c;
    }
    
    Observe that this would not be perfectly safe if we would have t++ anywhere since this would create a pointer to undefined memory location, but t + 1 is more OK. The only problem is that t+1 implicitly uses something similar to sizeof so it might look like cheating :wacky:
    Only my_sizeof_3 has its own logic applied because it creates objects first. But even there somebody should know the size of the object, so it is cheating anyhow.
    What I want to say, even if you don't use sizeof to find the size, the system this way or another must.
     
  14. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
  15. mohan kumar

    mohan kumar New Member

    Joined:
    May 22, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    #include<stdio.h>
    void main()
    {
    float i;

    float * p = &i;

    float * q= p;

    p++;

    printf("%d",int(p)-int(q));

    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice