# Consecutive Data Members

## Recommended Posts

Is there a way to control alignment of a C++ class members using standard C++ facilities without resorting to compiler-dependent directives, such as #pragma pack? What I want to do is making x, y, z addresses physically consecutive such that I can access the 3 elements using the subscript operator as follows: class myclass { public: float x, y, z; }; float *p = &x; p[0] is x p[1] is y p[2] is z Help appreciated.

##### Share on other sites
Quote:
 Is there a way to control alignment of a C++ class members using standard C++ facilities without resorting to compiler-dependent directives, such as #pragma pack?

No.

Quote:
 What I want to do is making x, y, z addresses physically consecutive such that I can access the 3 elements using the subscript operator as follows:

This, however, you can accomplish by making use of the fact that array elements are contiguous.

##### Share on other sites

Actually I saw this code in a released commercial game source, and what made me wounder whether this is possible that the "pack" alignment directive is missing...

##### Share on other sites
I think the C++ standard says that the compiler can't rearrange members variables in a class, which means they would be in order.

If they are packed tight or not is a function of enviroment (compiler, target machine, etc); chances are this game code was developed with a fixed enviroment in mind thus they knew it would work.

##### Share on other sites
You can configure such options globally on the command line, usually, so perhaps that was part of the game's build process. They also may be making assumptions since their target platform and build configuration is probably relatively rigid.

##### Share on other sites
Quote:
 Original post by phantomI think the C++ standard says that the compiler can't rearrange members variables in a class, which means they would be in order.

They can rearrange the order of members in a class, if they have different access specifiers. Not access levels, specifiers. Members within a specifier will have the same relative order, but there is no guaranteed order between members with different specifiers. So in the case of:
class Foo {  public: int a;  public: int b;};

a could come before b or b could come before a. Here:
class Foo {  public:     int a;    int b;  public:    int c;};

a must come before b, but the overall order could be c, a, b; a, c, b or a, b, c.

##### Share on other sites
Also, in case of inheritance, various vtables may be placed between different members.
struct Foo {  int x;};struct Bar : Foo {  int y;};
This could result in following layout:
- Bar_vtable- y- Foo_vtable- x

This is all compiler-specific stuff.

##### Share on other sites
I think there's something like [[align]] in the new upcoming C++ standard. I'm not sure if it gonna be there soon or at least compilers will be supporting this feature. That would be awesome.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978761

• 21
• 14
• 12
• 41