Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:11:38+00:00 2026-06-17T18:11:38+00:00

I recently ran into this question while I was working on a project, and

  • 0

I recently ran into this question while I was working on a project, and it confused me a bit. So I decided to write a test program to get a definitive 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's address" << endl;
    cout << t.GetaAddress() <<  "  : layer 2's address" <<endl;
    cout << t.GetDeepTLAddress() <<  "  : layer 3's 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 was happening before, this is the output:

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

How can all three of these objects share the same spot 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. Can someone please put me in my place and explain what’s going on here?

Edit: Perhaps it’s because I instantiated the objects in private rather than in the objects’ constructors? Baah I don’t know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:11:39+00:00Added an answer on June 17, 2026 at 6:11 pm

    The most definitive answer is that given by the C++ standard:

    Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise, they shall have distinct addresses.

    That is, if an object is a subject of another, they may have the same address.

    In C++11, a standard-layout struct (despite its name, that can be a class too) object’s first member is guaranteed to have the same address as the object itself:

    A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.

    Since your classes are all standard-layout, the behaviour you’ve observed is guaranteed by C++11.

    In C++03, the rule is similar, but applies to POD-struct types, rather than standard-layout struct types. Your classes, however, are not POD-struct types because they have user-defined destructors. So the behaviour you see here is not guaranteed by C++03.

    So why can this occur? Well all a class really is is a way to group some data together and provide operations on that data. Consider a class that just contains an int like so:

    class A
    {
      int x;
    };
    

    All this class is made up of is that int. When you create an object of type A, all you’re really doing is allocating enough space for its innards and initialising them (or in this case, not initialising them). Let’s say we create two instances of A:

    A a1;
    A a2;
    

    What do we have in memory? You could imagine that it looks like this:

       a1     a2
    ┌──────┬──────┐┄┄
    │  A   │  A   │
    └──────┴──────┘┄┄
    Memory ------->
    

    If we know that A just contains an int – that is, an A object is really nothing more than an int (except for maybe some padding) – then we know that the memory actually looks something like this if we break it down a bit more:

       a1     a2
    ┌──────┬──────┐┄┄
    │ int  │ int  │
    └──────┴──────┘┄┄
    Memory ------->
    

    You can see here that the A and int would both have the same address because the int is a subobject of the objects of type A. If A contained both an int and a char, it might look something like this:

           a1            a2
    ┌──────┬──────┬──────┬──────┐┄┄
    │ int  │ char │ int  │ char │
    └──────┴──────┴──────┴──────┘┄┄
    Memory ------->
    

    We know that char will have a higher address than the int because, once again, the standard says so:

    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.

    Note that a subobject does not necessarily share its address with the object its contained within, even if it’s the first one. It’s entirely up to the compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently was working with some dynamic Search Expressions and ran into a bit
Somewhat of an academic question, but I ran into this while writing some unit
I recently ran into this problem: Rails 3 link_to (:method => :delete) not working
I've recently ran into a bit of a pain. I've been using the JQuery
I recently ran into some issues this week on a web app that I
I recently ran into code that looks like this: next { 'foo' => bar,
G'day, I was using Test::Class perl module for some testing recently and ran into
I have recently ran into this strange issue, I was trying to reference parent
I ran into a problem recently and found out that this is a bona-fide
I ran into a real head scratcher recently while trying to debug an issue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.