How are objects stored in memory in C++?

前端 未结 5 1526
無奈伤痛
無奈伤痛 2020-12-03 02:18

How are objects stored in memory in C++?

For a regular class such as

class Object
    {
public:
    int i1;
    int i2;
    char i3;
    int i4;
pri         


        
5条回答
  •  甜味超标
    2020-12-03 02:38

    It really depends on the compiler, or rather, it is left up to the compiler to determine the memory layout.

    For instance, a mix of public, private, and protected member variables could be laid out such that each access type is contiguous. Or, derived classes could have member variables interleaved with unused space in the super class.

    Things get worse with virtual inheritance, where the virtually inherited base classes can be layed out anywhere in the memory allocated for that particular instance.

    POD is different because it needs to be compatible with C.

提交回复
热议问题