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

  • Home
  • SEARCH
  • 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 8699931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:04:11+00:00 2026-06-13T02:04:11+00:00

In parallel with this question: When should I use the new keyword in C++?

  • 0

In parallel with this question: When should I use the new keyword in C++?

Let’s say I have the following code structure:

class Foo{
private:
    int a;
    int b;
    /* ect */
};

class Bar{
private:
    Foo A;
    /* ect */
};

int main() {
    Bar *b;
    b = new Bar();

    // call b->methods()

    delete b;
};

I know from the link above b is heap (free store) allocated. But what about the contents of A inside class b? Is it safe to assume A also heap allocated?

  • 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-13T02:04:13+00:00Added an answer on June 13, 2026 at 2:04 am

    “On the heap” is, pedantically, a bit of a misnomer. C++ does not dictate the use of a heap, only that objects that are allocated using new are allocated on the unspecified “free-store”. Anyway…

    Yes, if B has a member variable of type A and you instantiate a B using new then all of that object is on the free-store.

    It’s important to note however that when you do:

    B* b = new B;

    b itself is not on the free-store, but is rather an automatic variable (ie “on the stack”). The thing that b points to is on the free-store. Automatic variables are destroyed when they go out of scope — but the thing b points to in this case will not be. If you don’t delete b;, you’ll get a memory leak.

    This may be confusing and seem unimportant. Maybe it is confusing, but it isn’t unimportant. The fact that b is an automatic variable that points to something on the free-store makes possible a very important idiom known as RAII, which is used for things like smart pointers.

    shared_ptr<B> b(new B);

    b, here, is still an automatic variable. But when b goes out of scope and is destroyed, it takes the thing b points to with it. Using smart pointers here will help to eliminate the memory leak created when you didn’t delete b above.

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

Sidebar

Related Questions

I have the following code: Parallel.ForEach(this.listView2.CheckedItems, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (CheckedItem)
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
I read this story on slashdot today where they announce a new parallel programming
I'm working with the new Task Parallel Library and today went to this case:
I have a method like this: public IOrganizationService GetConnection(bool multi) { if(!multi) { Parallel.For(0,
Please see this question for background information: How do Tasks in the Task Parallel
I have code that's somewhat like this: ($i=0; $i < 100; $i++) { do
This might be odd question but I am just goig to start a new
I saw this question somewhere Four processes p1, p2, p3, p4 - each have
this works: //delegate Parallel.For(1023456789, 1033456789, delegate(long i) { if (i % 10000000 == 0)

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.