substitute of sizeof operator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

substitute of sizeof operator

 
0
  #1
Sep 6th, 2005
I am back again with a new query...preparing for technical interview questions
---------------------------------
Without using sizeof operator,can we find sizeof datatype.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,412
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: substitute of sizeof operator

 
0
  #2
Sep 6th, 2005
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.
Last edited by Dave Sinkula; Sep 6th, 2005 at 3:02 pm. Reason: Changed 'structures' to 'objects'.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: substitute of sizeof operator

 
0
  #3
Sep 6th, 2005
actually awesome.....i tried it and it worked fine...thanx alot

here's the code for others
--------------------------
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a[3];
  6. char *ptr1,*ptr2;
  7. ptr1=(char*)&a[0];
  8. ptr2=(char*)&a[1];
  9. cout<<ptr2-ptr1;
  10. return 0;
  11. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: substitute of sizeof operator

 
0
  #4
Sep 6th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,412
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: substitute of sizeof operator

 
0
  #5
Sep 6th, 2005
Originally Posted by Stoned_coder
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: substitute of sizeof operator

 
0
  #6
Sep 6th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: substitute of sizeof operator

 
0
  #7
Sep 6th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: substitute of sizeof operator

 
0
  #8
Sep 6th, 2005
seems to work with c++ STL classes too
  1. #pragma warning(disable: 4786)
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. vector<string> array;
  10. array.resize(2);
  11. char* p1 = (char *)&array[0];
  12. char* p2 = (char *)&array[1];
  13. cout << p2-p1 << endl;
  14. cout << sizeof(array[0]) << endl;
  15. return 0;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: substitute of sizeof operator

 
0
  #9
Sep 6th, 2005
try with this class.....
  1. class hiddenaddr
  2. {
  3. public:
  4. void* operator &()const { return NULL;}
  5. };
or with this one
  1. class noaddrop
  2. {
  3. private:
  4. void* operator &()const;
  5. };
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.
Last edited by Stoned_coder; Sep 6th, 2005 at 11:03 pm. Reason: fix ma speling and const correctness was forgotten in a stoned daze
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: substitute of sizeof operator

 
0
  #10
Sep 7th, 2005
If you use an actual array, &array[0] is just a weird way of saying array.

  1. #include <iostream>
  2.  
  3. class hiddenaddr
  4. {
  5. public:
  6. void* operator &()const { return NULL;}
  7. };
  8.  
  9. class noaddrop
  10. {
  11. private:
  12. void* operator &()const;
  13. };
  14.  
  15. int main() {
  16.  
  17. hiddenaddr x[1];
  18. std::cout << ((char *)(x + 1)) - ((char *) x) << std::endl;
  19.  
  20. noaddrop y[1];
  21. std::cout << ((char *)(y + 1)) - ((char *) y) << std::endl;
  22.  
  23. return 0;
  24. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC