How can i find size of a variable without using sizeof() operator?

Showing Answers 1 - 6 of 6 Answers

praveen_n

  • Feb 11th, 2006
 

Suppose you want to find size of a variable A. Suppose its size is 1byte(8-bits) , then if you 'and ' with 255 i.e 'bit-wise and' (255&A) you will get A itself. So, to find if it's size is 1byte, if you 'bit-wise and ' the variable with 255(i.e, 2^(1*8)-1) and if you get same variable it means size is one-byte. If you wont get same variable , then try for 2-bytes i.e and with 2^16-1 (i.e, 2^(2*8)-1). If it gives the same variable then its size is 2-bytes. If not then try for 3-bytes...and so on.

  Was this answer useful?  Yes

Deepak Sharma

  • Feb 22nd, 2006
 

(log (complement(a)+a+1)/8) bytes

where base of the log is 2 and 'a' is any variable

  Was this answer useful?  Yes

arya

  • Mar 7th, 2006
 

consider we want to find size of A b and s ptr var of same type s= b=&A b++; b-s gives the size

  Was this answer useful?  Yes

Dan

  • Jun 6th, 2006
 

a generic method for doing this would betypedef int MyType;MyType *p = 0;return (int) ++p;of course, the ++ operator uses sizeof itself, but this is still a valid answer to an interview question in my opinion.

  Was this answer useful?  Yes

reshma

  • Sep 16th, 2006
 

What is the significance of using a char porinter in #define any_size(any) (char*)(&any)-(char*)((&any)-1) ?

Why can't we use any other pointer there?

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions