Stack vs. Heap (C++)

Started by
26 comments, last by alvaro 14 years, 10 months ago
Quote:Original post by alvaro

Now, if you have questions about any of the three issues above (stack-vs-heap, initializer lists and passing by value or by reference), try to not mix them up in your question.


The question was over how initializer lists should be employed. That was all that needed to be said -- "look up something called initializer lists". KulSeran did a nice job of it. I thank you for your help though, and your efforts to make me feel like a dumbass are truly appreciated.
~Argonaut________________________________Why "~Argonaut"? It's all just a mathematical expression denoting a close approximation of "Argonaut", which is irrational and can't be precisely defined.
Advertisement
Quote:Original post by argonaut
Quote:Original post by alvaro

Now, if you have questions about any of the three issues above (stack-vs-heap, initializer lists and passing by value or by reference), try to not mix them up in your question.


The question was over how initializer lists should be employed. That was all that needed to be said -- "look up something called initializer lists". KulSeran did a nice job of it. I thank you for your help though, and your efforts to make me feel like a dumbass are truly appreciated.


That was not the idea at all, and I am sorry if it came through as insulting. Lack of knowledge doesn't make you a dumb ass. I wrote my post hoping that it would help you structure your language and your thoughts about programming.

None of what you're really asking about has anything to do with stack vs heap. It's ALL on the stack here. The difference between these two is not about where it puts thing.
Foo::Foo(Bar _b) :bar(_b){}Foo::Foo(Bar _b){  bar = _b;}

The difference is about unnecessary work being performed (efficiency). The second one first calls the default constructor for bar, and then calls the asignment operator for bar. The assignment operator is reponsible for releasing the current things held by itself, as well as copying the new things to itself.
By using the top one of these two you avoid both the default initialisation, and the undoing of that default initialisaed state, and do only the constructing of the copied state.
(I'm assuming Bar is a class here, and potentially not a tiny class) It's even better to pass _b by reference.
It makes no difference if Bar were say a typedef for int though, but it's still good practice to use constructor initialisation lists every time you actually have a choice. Less code to improve upon if you were to change types later.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
If a variable is initialized with 'new' or malloc (or other alloc's) -> heap! And later on destroyed using 'delete' or free -> heap! Anything else -> stack!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Just to clarify, constructors DO NOT create objects. Their job is to initialize objects. How a particular object was created does not concern the constructor.
Quote:Original post by Decrius
If a variable is initialized with 'new' or malloc (or other alloc's) -> heap! And later on destroyed using 'delete' or free -> heap! Anything else -> stack!


Unless that variable is a member of a class, in which case it lives wherever the class was constructed.

class Foo{  Foo(int j) : i(j) {};   int i;}Foo * bar = new Foo(42); // i is on the heapFoo bar(42);  // i is on the stack


Note how the use of the initaliser list has no effect on where the member variable is stored.
There is more than stack and heap ;)
Foo x(42); // static storageint main(){    Foo y(42); // stack    Foo* z = new Foo(42); // heap    delete z;}

And isn't the heap actually managed by malloc/free, and new/delete manage the "free store"?

[Edited by - DevFred on June 19, 2009 9:41:26 AM]
Quote:Original post by argonaut
Specifically, with the : after the class constructor. Does that create the object on the stack or the heap?


That has nothing to do with it.

Google.

You were already shown the syntax for instantiating the class on the heap vs. on the stack. However, you should note that:

1) Member variables of a class all need to appear together; thus, if one is on the heap, they all are.

2) Things dynamically allocated by an object's implementation (e.g. the element storage allocated behind the scenes by std::vector) will always be on the heap, even if the object itself is on the stack, because it's dynamic allocation. (However, the object's data member which points to the dynamic allocation is still in the same place as the rest of the object.)

There's a good chance that none of the people yelling at you know what they're talking about, BTW.
Quote:Original post by DevFred
Just to clarify, constructors DO NOT create objects. Their job is to initialize objects. How a particular object was created does not concern the constructor.


Roughly speaking, allocate + initialize = create.

Quote:And isn't the heap actually managed by malloc/free, and new/delete manage the "free store"?


Yes. Most people can't be bothered to distinguish, though. Although they should.
Quote:
And isn't the heap actually managed by malloc/free, and new/delete manage the "free store"?

Something like that. The only way that is relevant is that you cannot deallocate pointers obtained from operator new using free(), and vice versa.

This topic is closed to new replies.

Advertisement