I am back again with a new query...preparing for technical interview questions
---------------------------------
Without using sizeof operator,can we find sizeof datatype.

Recommended Answers

All 11 Replies

My first reaction would be to say that sizeof is part of the language and go down that path...

But one technique that might be used is to declare an array of a least two of the objects, declare two pointers to char; then cast & assign the address of adjactent objects to each pointer; the difference between the pointers is the size of the object in bytes.

actually awesome.....i tried it and it worked fine...thanx alot

here's the code for others
--------------------------

#include<iostream>
using namespace std;
int main()
{
	int a[3];
	char *ptr1,*ptr2;
	ptr1=(char*)&a[0];
	ptr2=(char*)&a[1];
	cout<<ptr2-ptr1;
	return 0;
}

<< moderator edit: added [code][/code] tags >>

not guaranteed to work for all types. Some it will, some it wont. Theres no standard way of doing this. Its no coincidence that sizeof is a keyword.

not guaranteed to work for all types. Some it will, some it wont. Theres no standard way of doing this. Its no coincidence that sizeof is a keyword.

Could you elaborate, please?

Well actually now I begin to feel as if I was wrong. I didnt think you were allowed arrays of unions. But it actually seems you are. So as arrays are always contiguous it should actually work.
In 15 years I have never seen an array os unions. I thought they were prohibitted.

also any type with a user defined address of operator may cause trouble. Not impossible to get round but you wont be able to do it as above exactly.

seems to work with c++ STL classes too ;)

#pragma warning(disable: 4786)
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
	vector<string> array;
	array.resize(2);
	char* p1 = (char *)&array[0];
	char* p2 = (char *)&array[1];
	cout << p2-p1 << endl;
	cout << sizeof(array[0]) << endl;
	return 0;
}

try with this class.....

class hiddenaddr
{
   public:
      void* operator &()const { return NULL;}
};

or with this one

class noaddrop
{
   private:
      void* operator &()const;
};

For extra kudos (not open to Narue.... she has kudos in abundance already :) ) try to work out how to circumvent these operator overloads so that sizeof can be simulated for these classes too.

If you use an actual array, &array[0] is just a weird way of saying array.

#include <iostream>

class hiddenaddr
{
   public:
      void* operator &()const { return NULL;}
};

class noaddrop
{
   private:
      void* operator &()const;
};

int main() {

	hiddenaddr x[1];
	std::cout << ((char *)(x + 1)) - ((char *) x) << std::endl;

	noaddrop y[1];
	std::cout << ((char *)(y + 1)) - ((char *) y) << std::endl;

	return 0;
}

not my solution. I would have used an addressof() func written like so....

template<typename T>
T* addressof(T& val)
{
   return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(val)));
}

and would have used that.
Kinda a cheat to use the array name to miss the overloaded & operator.

now how do you simulate sizeof for a singleton? cant put those in arrays, as by definition only 1 will exist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.