0

If we have the following code:

struct Base
{
    int x;
    int y;
    void foo();
    virtual unsigned getCrc() = 0;
};

struct Derived1 : public Base
{
    int a;
    int b;
    unsigned getCrc();
};

struct Derived2 : public Base
{
    float a;
    float b;
    unsigned getCrc();
};

Is it C++ standard that a and b should be after x and y in memory? Or it is the most used method for laying out inherited objected? (i.e. compiler defacto-standard).

In other words, can I guarantee that:

Derived1 obj;

    int* unsafe_internal_a = (int*)((unsigned)(&obj) + sizeof(Base));

EDIT: My question is 'Is memory layout covered in some standard? Or it is compiler dependent?'. The code is just for illustration.

Yousf
  • 3,957
  • 3
  • 27
  • 37
  • AFAIK, `a` and `b` will always be after `x` and `y` but there can be padding bytes added anywhere the compiler chooses to. – Alok Save Feb 10 '13 at 14:58
  • The order of the members in a structure or class is retained, padding may be introduced between them, though. –  Feb 10 '13 at 15:00
  • 1
    Are you getting the correct value of _a_? Isn't _Derived1_ supposed to also have a pointer to vtable? – Zlatomir Feb 10 '13 at 15:01
  • 1
    You are trying to access the private members of the base class? That is suspect in itself, and hints at some design flaws. :-) What are you *really* trying to do? – Bo Persson Feb 10 '13 at 15:19
  • Edited to access public members and edit code example. – Yousf Feb 10 '13 at 15:42

2 Answers2

1

The answer to your question is: they are compiler dependent in certain situations, and not in others. Details within

If you need to extract data from a class, and compact it to a minimum format, I suggest implementing a set of serialize/deserialize methods for them. If you are just trying to figure out what c++ does, the question linked should help a lot.

Community
  • 1
  • 1
David D
  • 1,571
  • 11
  • 12
  • Memory layout article talks about Microsoft Visual C++. MSVC is the farthest compiler from standards. – Yousf Feb 10 '13 at 16:32
  • @Yousf, I agree that MSVC may not be the most compliant compiler, but the answer linked has more than just the MSVC explanation. – David D Feb 10 '13 at 17:14
0

This would be safer

size_t off = offsetof( Derived1, a );
Derived1 obj;

unsigned* unsafe_internal_a = (unsigned*)((char *)(&obj) + off);
  • 2
    No, [offsetof will only work for POD objects](http://stackoverflow.com/questions/1129894/why-cant-you-use-offsetof-on-non-pod-strucutures-in-c) – Bo Persson Feb 10 '13 at 15:04