Sign in to follow this  
Sambori

Consecutive Data Members

Recommended Posts

Sambori    100
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 this post


Link to post
Share on other sites
jpetrie    13099
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.

You could also just implement an operator[] overload for your class.

Share this post


Link to post
Share on other sites
Sambori    100
Thanks for your reply.

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 this post


Link to post
Share on other sites
_the_phantom_    11250
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 this post


Link to post
Share on other sites
jpetrie    13099
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 this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by phantom
I 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 this post


Link to post
Share on other sites
Antheus    2409
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 this post


Link to post
Share on other sites
Sambori    100
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this