Consecutive Data Members
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.
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.
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...
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...
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.
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.
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.
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.
Also, in case of inheritance, various vtables may be placed between different members.
This is all compiler-specific stuff.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement