Sign in to follow this  

Consecutive Data Members

This topic is 2787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


Link to post
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.

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

Share this post


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

This topic is 2787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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