| | |
substitute of sizeof operator
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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
actually awesome.....i tried it and it worked fine...thanx alot
here's the code for others
--------------------------
<< moderator edit: added [code][/code] tags >>
here's the code for others
--------------------------
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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.
•
•
•
•
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.
"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
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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.
In 15 years I have never seen an array os unions. I thought they were prohibitted.
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
try with this class.....
or with this one
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.
C++ Syntax (Toggle Plain Text)
class hiddenaddr { public: void* operator &()const { return NULL;} };
C++ Syntax (Toggle Plain Text)
class noaddrop { private: void* operator &()const; };
) 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
If you use an actual array, &array[0] is just a weird way of saying array.
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ reading,writing,displaying a file?
- Next Thread: C++ Object Pointers Problem
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






