|
-
April 21st, 2004, 02:27 AM
#1
structure of a struct...
Suppouse I have a struct/class for example:
struct cMyStruct
{
int x;
char y;
double z;
char abc[5];
char ser[6];
};
and I'm sending it/or a pointer of it as a function parameter,
for example void MyFunc(cMyStruct* myStruct)
Is there a way for the function, to the know the structure of the struct?
Is there a way to know the offset of each field in the struct?
Thanks
avi
-
April 21st, 2004, 02:58 AM
#2
Code:
struct cMyStruct s;
int offset_of_abc = (char*)(&s.abc) - (char(*)(&s);
-
April 21st, 2004, 02:58 AM
#3
giving
MyFunc(cMyStruct* myStruct)
the offset of each field is
(unsigned char*)&(myStruct->fieldName) - (unsigned char*)myStruct
**** **** **** **** **/**
-
April 21st, 2004, 02:59 AM
#4
but if I don't know the name of the field abc, is there a way to get all the class offsets?
Thanks
avi
-
April 21st, 2004, 05:59 AM
#5
Since you are passing the struct as a cMyStruct* to the function, how can you not know? The function will recieve a cMyStruct and since member-variable-names are known at compile time, just check the definition/documentation for the names. The number of, and the name of members can't magically change during run-time.
-
April 21st, 2004, 07:01 AM
#6
of course I can look at the structure definition...
but I wanted to know if there is a way to get the address of each of the fields without using thier names (I want it to be general for a few structs)
Thanks
avi
-
April 21st, 2004, 07:03 AM
#7
How would you specify which field you wanted?
-
April 21st, 2004, 07:06 AM
#8
I don't know, that's what I want to know, is it possible?
Is there a function that may return the intrire strcture of the struct (I mean holds x fields, with the offsets/address...)
Anyone?
Thanks again
avi
-
April 21st, 2004, 07:13 AM
#9
That information is not preserved by a C++ compiler.
The compiler just creates machine code instructions for each structure access you do.
Something like:
struct ms {int a; long b};
ms s;
s.b = 1;
becomes something like:
mov s(4), #1 (move 1 into 'the address that s is at plus 4').
So the compiler works out that member b is at offset 4 (because a is four bytes longs and b comes after it), and hard codes that whenever you access the structure.
-
April 21st, 2004, 07:22 AM
#10
ok, then is there a way to know the type of the fields in the structure, I mean is there a way to get for example: int, int, char[4], char[6]
for the struct:
struct cMyStruct
{
int x;
int y;
char z[4];
char z[6];
}
Thanks
avi
-
April 21st, 2004, 10:32 AM
#11
Nope. Once it's in machine code, you cannot. You might be able to guess at what the object is, by the length and the data it contains (that is, if you can tell what the data really is), but that's all you'd be doing is guessing.
Nothing about the structure is preserved (like Zaccheus said). So, how *could* you know what the fields are?
Viggy
-
April 21st, 2004, 01:38 PM
#12
I'm not sure what you are trying to do,
this code might help. if you decide to use it, then you have
to implement the virtual function for each structure/class
according to its data members.
Code:
#include <vector>
#include <string>
#include <typeinfo.h>
#include <iostream.h>
using namespace std;
struct A{
char a[20]; long b; short c; char d;
// vector contains pairs of member typename and offset
// DO NOT FORGET A VPTR IS ADDED TOO TO AN OBJECT
virtual vector<pair<string,int> > structure()
{
vector<pair<string,int> > descVec;
descVec.push_back(make_pair(typeid(a).name(), (char*)&(this->a) - (char*)this));
descVec.push_back(make_pair(typeid(b).name(), (char*)&(this->b) - (char*)this));
descVec.push_back(make_pair(typeid(c).name(), (char*)&(this->c) - (char*)this));
descVec.push_back(make_pair(typeid(d).name(), (char*)&(this->d) - (char*)this));
return descVec;
}
};
int main()
{
A obj;
vector<pair<string,int> > descVec = obj.structure();
for(int i=0; i < descVec.size(); i++)
cout << "TYPE: " << descVec[i].first.c_str() << endl << "OFFSET: " << descVec[i].second << endl << endl;
return 0;
}
you can add information about sizeof() of each member, but then,
pair would have to be replaced by other container.
Last edited by Guysl; April 21st, 2004 at 02:42 PM.
**** **** **** **** **/**
-
April 22nd, 2004, 03:07 AM
#13
Originally posted by Guysl
... you can add information about sizeof() of each member, but then, pair would have to be replaced by other container.
You could try this slightly wacky concoction:
pair<string, pair<int,size_t> >
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|