CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    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

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Code:
    struct cMyStruct s;
    int offset_of_abc = (char*)(&s.abc) - (char(*)(&s);
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    giving

    MyFunc(cMyStruct* myStruct)


    the offset of each field is
    (unsigned char*)&(myStruct->fieldName) - (unsigned char*)myStruct
    **** **** **** **** **/**

  4. #4
    Join Date
    Sep 2003
    Posts
    815
    but if I don't know the name of the field abc, is there a way to get all the class offsets?

    Thanks
    avi

  5. #5
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    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.

  6. #6
    Join Date
    Sep 2003
    Posts
    815
    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

  7. #7
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492
    How would you specify which field you wanted?
    My hobby projects:
    www.rclsoftware.org.uk

  8. #8
    Join Date
    Sep 2003
    Posts
    815
    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

  9. #9
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492
    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.
    My hobby projects:
    www.rclsoftware.org.uk

  10. #10
    Join Date
    Sep 2003
    Posts
    815
    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

  11. #11
    Join Date
    Feb 2002
    Posts
    4,640
    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

  12. #12
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    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.
    **** **** **** **** **/**

  13. #13
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492
    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> >
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured