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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:48:32+00:00 2026-05-25T23:48:32+00:00

Possible Duplicate: Does this type of memory get allocated on the heap or the

  • 0

Possible Duplicate:
Does this type of memory get allocated on the heap or the stack?

I have a few memory-related questions, I was hoping you guys would be able to answer. Consider this code, with Foo representing a large class with lots of methods and lots of primitive data members:

class Foo {
  public:
    Foo() : // Initialize all data members ...
        { }

    // Lots of methods ...

    ~Foo() { /*Nothing needed here ... correct?*/ }

private:
    int    a;
    int    b;
    char   c;
    double d;
    // Lots of other primitive data members (no pointers) ...
};

class Bar {
  public:
    void func() {
        foo = new Foo();
    }

    // Assuming Bar::func() is always called before deletion ...
    ~Bar() { delete foo; }
  private:

    Foo* foo;
};

int main() {
    Bar bar;
    bar.func();

    Bar* barptr;
    barptr->func();

    return 0;
}

My question is, when I call bar.func(), are all the data members of Foo allocated on the stack memory or on the heap memory (I know foo would be on the heap…I think!)? What about when I call barptr->func()? Is, for example, Foo::a on the stack or on the heap?

Furthermore, do I need to explicitly delete anything in Foo::~Foo()? Foo only has primitive local data members, and none of the functions use new or malloc().

In the case that I have absolutely no idea what I’m talking about, I’d appreciate it if someone could explain where these data are stored.

  • 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-05-25T23:48:33+00:00Added an answer on May 25, 2026 at 11:48 pm

    Everything allocated with new is on the heap, including all its members. The line foo = new Foo(); creates a Foo (including all its members) on the heap.
    Only named objects are on the stack. In your case bar (a Bar), and barptr (a pointer to a Bar).
    Foos destructor doesn’t need to do anything, which is actually quite common, if you use RAII properly, like is the C++ way.

    In your main function, bar is a region on the stack that is a Bar, which allocates a Foo on the heap. If barptr; were actually initialized, barptr would be a region on the stack that is a pointer to a Bar somewhere (maybe stack maybe not). However, in every case, a Bars foo member will be allocated on the heap, since that’s what Bars constructor tells it to do.

    In your main function, bar is a region on the stack that is a (pointer to a (large collection of primitives)), which allocates a (large collection of primitives) on the heap.
    If barptr; were actually initialized, barptr would be a region on the stack that is a pointer to a (pointer to a (large collection of primitives)) somewhere (maybe stack maybe not). However, in every case, the (large collection of primitives) will be allocated on the heap, since that’s what the constructor tells it to do.

    Since I know someone will mention it, the heap/stack distinction is at the compiler’s whim, and not standardized in any way. It is fully allowed to “cheat” and put things in other places if it feels like it. For instance, global objects are frequently on neither the stack nor the heap, but in another place. Also, that’s how C# and java go so fast despite the frequent (apparent) use of memory management. The compiler frequently “cheats” and puts “dynamically scoped” objects on the stack.

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

Sidebar

Related Questions

Possible Duplicate: How does .net managed memory handle value types inside objects? I have
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What does a type followed by _t (underscore-t) represent? While typing in
Possible Duplicate: What does this C++ code mean? I'm trying to map a C
Possible Duplicate: Java - why no return type based method overloading? The compiler does
Possible Duplicate: How does this work? Weird Towers of Hanoi Solution While surfing Google,
Possible Duplicate: C programming, why does this large array declaration produce a segmentation fault?
Possible Duplicate: What does jQuery.fn mean? I have some script on a page trying
Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
Possible Duplicate: Reference - What does this symbol mean in PHP? Is there any

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.