vendredi 24 avril 2015

Memory allocation for public and private fields

This is not a duplicate of this question, I read the answers and I still have some questions about this subject.

I tested some classes like this one:

class A {
private:
    int b;

public:
    char c;
    int a;

private:
    char e;
};

And I've seen that the fields are stored as if there were no access-specifier, this is not wrong since :

N3376 (the first post C++11 draft) 9.2 [class.mem]/13:

Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified. Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

Why I still don't understand is this :

The order of allocation of non-static data members with different access control is unspecified.

What do they mean by unspecified, they certainly have a way of storing this, they don't just do it randomly I guess... They don't want the user to know it ? Or there is so many ways depending on the options maybe ?

Because all the examples I tried, I had the same order as declared in my file (+ padding)

I need to know this because I am creating a program that calculates the padding between all fields, the program works with structures that have no access specifiers for the moment. I will have to find a way to do this when there is different accessibility blocks

I am using gcc 4.9.2

Aucun commentaire:

Enregistrer un commentaire