What is the memory layout of a C++ Class object?
It depends on the following factors:
- Size of all non-static data members
- Order of data members
- Byte alignment or byte padding
- Size of its immediate base class
- The existence of virtual function(s)
- Compiler being used
- Mode of inheritance (virtual inheritance)
For non-static data members, it is basically similar to C
struct, the order in which you
declare the data members of a class is the order they will be put/align in the memory.
This concept is important for debugging memory problems. The first and last data member are stored firstly and lastly in the memory space of a Class object, so they can be served as sentinels to protect the data integrity of a class. Check them often (in member functions) then you can get more clear picture of when data in a Class is being modified.