5

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).

What 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, GCC certainly have a way of doing 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 am using gcc 4.9.2, Does anyone know if GCC specified their way of doing this ?

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

Community
  • 1
  • 1
Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
  • 2
    If something is "unspecified" or "implementation specific" that means that the compiler can basically do as it pleases, as long as it doesn't break anything else. – Some programmer dude Apr 24 '15 at 08:10
  • 3
    http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior – Mat Apr 24 '15 at 08:10
  • 3
    @JoachimPileborg: Not quite: Implementation defined means that the implementation is free to choose, but must document the choice to the user. Unspecified means that user has no right to know. In this example, the compiler to shuffle the class layout differently during every compilation, and that would be conforming. – Kerrek SB Apr 24 '15 at 08:11
  • unspecified behavior: behavior, for a well-formed program construct and correct data, that depends on the implementation. The implementation is not required to document which behavior occurs. – Alper Apr 24 '15 at 08:12
  • I think that the idea is to allow compilers to optimize private data members (possibly eliding some of them by the as-if rule), using the definition of all the member functions. It would be nice to have this feature, but AFAIK no compiler actually does that. – sbabbi Apr 24 '15 at 08:16
  • This also means that gcc has a way of doing this, but I have no right to know ? Did I understand well ? – Othman Benchekroun Apr 24 '15 at 08:17

1 Answers1

3

By unspecified means the compiler is:

  • free to make any decision it likes
  • and is not required to document it.

And implementation-defined means, the compiler is free to make any decision, and is required to document it..


If you consider this class (slightly modified your version):

class X {
private:
    int b;
    int z;

public:
    char c;
    int a;

private:
    char e;

public:
    int d;
};

then the text from the spec means:

  • It is guaranteed that
    • &c < &a — both belong to same access control.
    • &b < &z — both belong to same access control.
  • It is also guaranteed that
    • &z < &e — both belong to same access control, with interleaving.
    • &a < &d — both belong to same access control, with interleaving.
  • It is not guaranteed that:
    • &z < &c — both belong to different access control.
    • &a < &e — both belong to different access control.

I've seen code which uses access specifier for each variable, so that the compiler can re-arrange them in order to make the size as smaller as possible.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • I think that `&z < &e` and `&a < &d` are guaranteed, it's just for different specifiers that it is not specified – Othman Benchekroun Apr 24 '15 at 08:21
  • @Othman: I thought that as well. But I've heard the argument that they're considered different even if they are same of *kind* and the argument is that it helps compiler to optimize `(sizeof(X))` as much as possible. – Nawaz Apr 24 '15 at 08:23
  • 1
    @Othman There was a change between C++03 and C++11 for this. C++03 only guarantees ordering between "nonstatic data members of a (non-union) class declared without an intervening *access-specifier*"; C++11 guarantees ordering between all data members with the same access control. – T.C. Apr 24 '15 at 08:26
  • But can I still find a specific behavior of gcc in this case ? – Othman Benchekroun Apr 24 '15 at 08:30
  • @Othman: Yes, obviously GCC makes some decision. But I'm not sure if GCC has documented *that* decision which you can see, because it is not required by the language to do so (which is why it is called *unspecified*). – Nawaz Apr 24 '15 at 08:31
  • @T.C.: The question is, what is meant by *same access control*? if I use `public` twice in my class, are they same or different (in the context of allocation of members)? – Nawaz Apr 24 '15 at 08:34
  • They are the same. Different *access-specifier*, same access control. – T.C. Apr 24 '15 at 08:41
  • @T.C.: if that is true, then my answer needs to be corrected. and now it is corrected. – Nawaz Apr 24 '15 at 08:45
  • @Nawaz Do you know where can I find out if GCC specifies their decision ? – Othman Benchekroun Apr 24 '15 at 08:48
  • 1
    @Othman: No idea. I never needed that. Why do you care that much? For knowledge sake it is okay, but if you're going to write code on that, then it would be a dangerous thing to do. – Nawaz Apr 24 '15 at 08:50
  • @Nawaz As I explained in my post, I am doing a program that calculates padding of classes ( I try to find the best order that minimize the size exactly), it was pretty fine until I find out about this unspecified thing ... – Othman Benchekroun Apr 24 '15 at 08:54