Structure/Class member addresses
-
struct myStruct {
int x;
int y;
char ch[10];
};
struct myOtherStruct {
int y;
char ch[10];
int x;
};
class myClass {
public:
myClass() { };
~myClass() { };void someMethod(int x) { /\*do something with x\*/; };
private:
int a;
int b;
char ch[10];
};int main() {
myStruct s1; myOtherStruct s2; myClass c; return 0;
};
Given these struct/class definitions, is it always safe to assume:
&s1.x < &s1.y < &s1.ch &s2.y < &s2.ch < &s2.x &c.a < &c.b < &c.ch
(Considering the addresses as numbers, like you'd get from(__int64)&s1.x
) Maybe an easier way to ask this is, "Is the order of the members in memory the same as the order in which they are declared?" Thanks in advance for any help you guys can offer!Mike the Red wrote:
Is the order of the members in memory the same as the order in which they are declared?
No, absolutely not. Without additional information, any compiler can do as it pleases, as long as it is consistent. Here are two typical approaches: 1. reorder the items according to element size, possibly reducing struct size: since items want to be "naturally aligned" (i.e. a N-byte quantity starts at an address that is a multiple of N for best performance) it may pay off to put the largest ones first. 2. keep the declaration order and pad with dummy bytes to achieve natural alignment. Not a single strategy is good for all purposes as one may want compatibility with another language, an existing data structure in a file, a set of registers in a hardware device, etc. Hence, most compilers have switches and/or pragma's to control the exact behavior. You should read the documentation of your tools. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
struct myStruct {
int x;
int y;
char ch[10];
};
struct myOtherStruct {
int y;
char ch[10];
int x;
};
class myClass {
public:
myClass() { };
~myClass() { };void someMethod(int x) { /\*do something with x\*/; };
private:
int a;
int b;
char ch[10];
};int main() {
myStruct s1; myOtherStruct s2; myClass c; return 0;
};
Given these struct/class definitions, is it always safe to assume:
&s1.x < &s1.y < &s1.ch &s2.y < &s2.ch < &s2.x &c.a < &c.b < &c.ch
(Considering the addresses as numbers, like you'd get from(__int64)&s1.x
) Maybe an easier way to ask this is, "Is the order of the members in memory the same as the order in which they are declared?" Thanks in advance for any help you guys can offer!C++ standard says that ordering of variables with in a single access block must be in the order they appear but different blocks can be arranged in arbitrary order. For example:
class Foo
{
public:
int x;
int y;private:
int _x;
int _y;
};address of x < address of y address of _x < address of _y But you cannot determine relation between address of x and _x. Compiler is free to place then in any order. -Saurabh
-
C++ standard says that ordering of variables with in a single access block must be in the order they appear but different blocks can be arranged in arbitrary order. For example:
class Foo
{
public:
int x;
int y;private:
int _x;
int _y;
};address of x < address of y address of _x < address of _y But you cannot determine relation between address of x and _x. Compiler is free to place then in any order. -Saurabh
I don't think so. Can you provide a link to the section that says so? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I don't think so. Can you provide a link to the section that says so? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Okay I admit I made the comment based on excellent book "Inside the C++ Object Model". But I check the C++ standard and it is there in Section 9.2 point 12: "Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members separated by an access-specifier is unspecified (11.1). 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)." and Section 11.1 point 3: "The order of allocation of data members with separate access-specifier labels is unspecified (9.2)." -Saurabh
-
Okay I admit I made the comment based on excellent book "Inside the C++ Object Model". But I check the C++ standard and it is there in Section 9.2 point 12: "Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members separated by an access-specifier is unspecified (11.1). 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)." and Section 11.1 point 3: "The order of allocation of data members with separate access-specifier labels is unspecified (9.2)." -Saurabh
Thanks. So it says it is not always safe to assume the order is preserved. That is what I remembered, however I didn't know the exceptions were documented as well. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
struct myStruct {
int x;
int y;
char ch[10];
};
struct myOtherStruct {
int y;
char ch[10];
int x;
};
class myClass {
public:
myClass() { };
~myClass() { };void someMethod(int x) { /\*do something with x\*/; };
private:
int a;
int b;
char ch[10];
};int main() {
myStruct s1; myOtherStruct s2; myClass c; return 0;
};
Given these struct/class definitions, is it always safe to assume:
&s1.x < &s1.y < &s1.ch &s2.y < &s2.ch < &s2.x &c.a < &c.b < &c.ch
(Considering the addresses as numbers, like you'd get from(__int64)&s1.x
) Maybe an easier way to ask this is, "Is the order of the members in memory the same as the order in which they are declared?" Thanks in advance for any help you guys can offer!I thought I recalled what Luc Pattyn said, but went searching and basically came up with the same thing that Saurabh has presented. There is also a difference between the current C++ standard and the draft of the next standard. The current guarantees order only until the next access-specifier specifier. The text in the draft for the new standard is: 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 (11). So to extend Saurabh's example:
class Foo { public: int x1; int y1; private: int x2; int y2; public: int x3; int y3; };
The draft for the new standard (unless its been changed from the one I looked at :) ) guarantees the order x1 - y1 - x3 - y3, while under the current standard the storage order of x1 and x3 is unspecified.Please do not read this signature.
-
Thanks. So it says it is not always safe to assume the order is preserved. That is what I remembered, however I didn't know the exceptions were documented as well. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
In the example the OP submitted it is always safe. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
In the example the OP submitted it is always safe. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]"always" and "example" indicate an interest in the general answer, covering a broad domain. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
"always" and "example" indicate an interest in the general answer, covering a broad domain. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Please don't cheat, his question is actually very well defined. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Please don't cheat, his question is actually very well defined. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]do you really believe the code shown is the actual code? that was only the tip of some iceberg; we can only guess what stuff is hiding below the surface. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
do you really believe the code shown is the actual code? that was only the tip of some iceberg; we can only guess what stuff is hiding below the surface. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Of course I don't believe that's the actual code and I'm pretty sure it hides tons of code behind. Anyway my point is still valid (and you know that ;P). BTW it is friday, have a :beer: or two.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]