2

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 3
    What else could it mean for `b` to be allocated on the free store but that _its contents are allocated in the free store_? – Seth Carnegie Oct 19 '12 at 22:32
  • 2
    In fact `b` is not on the free store. `b` is a pointer, and it's an automatic variable in the function `main`. Once it has been assigned to in the second line of `main`, it points to memory allocated from the free store. Pedantic difference, and I expect you already know about it, but enough people come past SO who are *deeply* confused that I think it's worth keeping such things straight :-) Likewise there is no class `b`, it's class `Bar`, and the referand of `b` is an instance of that class. – Steve Jessop Oct 19 '12 at 22:45
  • 1
    @SteveJessop yes I know, I was using the terminology he was using so he could understand. But your point is well-taken. Perhaps it would be better to say `*b` is allocated on the free-store. – Seth Carnegie Oct 19 '12 at 23:18
  • @Seth: oops, sorry, I intended to address the questioner, and specifically I was responding to the remark "`b` is heap (free store) allocated". You were just in the cross-fire, I didn't realize my comment can be read as a response to yours. – Steve Jessop Oct 19 '12 at 23:22
  • @SteveJessop haha no problem, text is ambiguous :) But the comment is correct even if it is read as a response to mine, so no worries. – Seth Carnegie Oct 19 '12 at 23:31
  • @SethCarnegie Thanks for correcting me: "Perhaps it would be better to say *b is allocated on the free-store." Makes sense to me. Not sure if I should edit my question to reflect the right terminology; but I want other to learn from my error. –  Oct 19 '12 at 23:51

1 Answers1

8

"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.

John Dibling
  • 99,718
  • 31
  • 186
  • 324