Do objects located within other objects have the same address?

I recently came across this issue when I was working on a project, and it confused me a bit. So I decided to write a test program to get the final answer:

#include <iostream>

using namespace std;

class layer3{
public:
    layer3(){}
    ~layer3(){}     
private:

};


class layer2{
public:
    layer2(){}
    ~layer2(){}

    layer3* GetBAddress(){return &b;}
private:
    layer3 b;
};


class layer1{
public:
    layer1(){}
    ~layer1(){}

    //returns the address of a, which is a 'layer2' object
    layer2* GetaAddress(){return &a;}
    //returns the address of b, which is is a layer 3 object
    layer3* GetDeepBAddress(){return a.GetBAddress();}
private:
    layer2 a;

};

int main(){

    layer1 t;
    cout << &t << "  : layer 1 address" << endl;
    cout << t.GetaAddress() <<  "  : layer 2 address" <<endl;
    cout << t.GetDeepTLAddress() <<  "  : layer 3 address" <<endl;

}

This program creates 3 objects. layer2 is created inside layer1, and layer3 is created inside layer2. Then I call to get the addresses of layer1, layer2 and layer3, and just as it was before, this is the output:

$ ./a.exe
0x28ac4f  : layer 1 address
0x28ac4f  : layer 2 address
0x28ac4f  : layer 3 address

How can all three objects have the same place in memory? What if I scaled this program to have 50 layers (objects)? Or 10,000? I'm not quite sure how this is possible. Could someone please put me in my place and explain what is going on here?

: , , , ? .

+5
4

, ++:

, , , , ; .

, , .

++ 11 ( , , class ), , , :

, reinterpret_cast, ( , , ) .

, , , ++ 11.

++ 03 , POD-, . POD-struct, . , , , ++ 03.

? , - . , int :

class A
{
  int x;
};

, int. A, , , ( , ). , A:

A a1;
A a2;

? , :

   a1     a2
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”β”„β”„
β”‚  A   β”‚  A   β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜β”„β”„
Memory ------->

, A int, A , int ( , , ), - , - , :

   a1     a2
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”β”„β”„
β”‚ int  β”‚ int  β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜β”„β”„
Memory ------->

, A int , int A. A int, char, :

       a1            a2
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”β”„β”„
β”‚ int  β”‚ char β”‚ int  β”‚ char β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜β”„β”„
Memory ------->

, char , int, , , :

(non-union) ( 11) , .

, , , . .

+12

() ( ).

, layer1 layer2, , , layer1 (, , layer1) layer2. layer2 layer3.

+3

, . , , , , . , , .

0

This is how objects are stored in memory. C ++ only gives some guarantees of how member objects are ordered - a lot depends on the compiler - alignment, including members around (subject to restrictions). Why add memory elements or offsets when you don't need it? If the addresses do not match, you will have objects with unused memory.

As a test, you can add a function virtualto layer2and see what happens.

0
source

All Articles