Is Memory Layout for a class successive?
When we declare object of a class is its memory layout successive(One after 开发者_如何学Cthe other)?If its successive than does padding occurs in it (like structure padding)?Please help me out with the concepts of memory layout for a class
Thanks in advance.
When we declare object of a class is its memory allocation successive(One after the other)?
The Standard doesn't give any such guarantee. Object memory layout is implementation-defined.
Usually, memory address for data members increases in the order they're defined in the class . But this order may be disrupted at any place where the access-specifiers (private
, protected
, public
) are encountered. This has been discussed in great detail in Inside the C++ Object Model by Lippman.
An excerpt from C/C++ Users Journal,
The compiler isn't allowed to do this rearrangement itself, though. The standard requires that all data that's in the same public:, protected:, or private: must be laid out in that order by the compiler. If you intersperse your data with access specifiers, though, the compiler is allowed to rearrange the access-specifier-delimited blocks of data to improve the layout, which is why some people like putting an access specifier in front of every data member.
Interesting, isn't it?
精彩评论