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 8024085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:45:33+00:00 2026-06-04T22:45:33+00:00

Let’s say we have 4 classes as follows: class A { public: A(void) :

  • 0

Let’s say we have 4 classes as follows:

class A
{
    public:           
        A(void) : m_B()
        {
        }
    private:
        B m_B;
}

class B
{
    public:            
        B(void)
        {
           m_i = 1;
        }
    private:
        int m_i;
}

class C
{
    public:           
        C(void) 
        {
            m_D = new D();
        }
        ~C(void) 
        {
            delete m_D;
        }
    private:
        D *m_D;
}

class D
{
    public:           
        D(void)
        {
           m_i = 1;
        }
    private:
        int m_i;
}

Lets say there are 4 cases:

case 1: A externally allocated on the stack, B internally allocated on the stack

A myA1;

case 2: A externally allocated on the heap, B internally allocated on the stack

A *myA2 = new A();

case 3: C externally allocated on the stack, D internally allocated on the heap

C myC1;

case 4: C externally allocated on the heap, D internally allocated on the heap

C *myC2 = new C();

What goes on in each of these cases? For example, in case 2, I understand the pointer myA2 is allocated on the stack, the A object exists in the heap, but what about the m_B attribute? I assume space on the heap allocated for it as well because it wouldn’t make sense for an object to exist in heap space and then it’s attribute goes out of scope. If this is true then does that mean the external heap allocation overrides the internal stack allocation?

What about case 3, myC1 is allocated on the stack, however m_D is allocated on the heap. What happens here? Are the two parts split across memory? If I removed the ‘delete m_D’ from the destructor and myC1 went out of scope, would there be a memory leak for the space allocated on the heap for m_D?

If there are any tutorials/articles that go over this in detail I would love a link.

  • 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-04T22:45:35+00:00Added an answer on June 4, 2026 at 10:45 pm

    I think that you are confusing "stack/heap allocation" and "automatic variable".

    Automatic variables are automatically destroyed when going out of context.

    Stack allocation is the fact that the memory is allocated on the execution stack. And variable allocated on the stack are automatic variables.

    Also, members are automatic variables whose destructors get called when its owner is destroyed. In the case of pointers, they are destroyed but not the underlying object, you have to explicitly call delete. To make sure that the underlying object is destroyed you have to use smart or unique pointers.

    To put it another way: variables/members that you have to call delete on, are not automatic variables.

    Lastly, member of a class are allocated on the same memory segment of the its owner.

    In you code:

    • A.m_B is an automatic variable. If A is on the stack so is B and if A is on the heap so is B.
    • B.m_i and D.m_i are an automatic variables and will be allocated on the same memory segment of their owner
    • The pointer C.m_D is an automatic variable, but the pointed object of type D is not, you have to explicitly call delete on the pointer to delete the underlying object. So, the pointer C.m_D is allocated on the same memory segment, but not the underlying objet. It’s cleary allocated by new and will be on the heap.

    So:

    • Case 1: Everything is on the stack and automatic (ie: destroyed automatically).
    • Case 2: myA2 is on the heap and not automatic (you have to delete myA2). Its member m_B2 is an automatic variable that will be destroyed when myA2 is destroyed. Also since myA2 is on the heap, m_B, like any member of a class, is in the same memory space the heap too.
    • Case 3: myC1 is on the stack and is an automatic variable, The pointer to m_D is on the stack too, but not the object pointed by m_D which is allocated by new on the heap.
    • Case 4: Same as case3 but myC2 is on the heap and is not automatic. So you have to delete myC2 (which will delete m_D).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say I have the following classes : public class MyProductCode { private String
Let's say we have the following: abstract class A; class B : public A;
Let's say we have 3 classes: A, B and C. Each class has the
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say that I have a date in R and it's formatted as follows.
Let's say I have one class User, and it has a property of type
Let's say I have two Entity classes: SocialApp and SocialAppType In SocialApp I have
Let's say you have a class library project that has any number of supplemental
Let's say I have an abstract parent class called shape, and that there are

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.