Usual Stack Memory
This is familiar: Declared variables are allocated on the stack, and are removed when the block exits. There are no new C++ features in this code, but some comments point out stack allocations and deallocations, which you may not have thought about.
/* * This is the familiar creation of objects by declaration. They * are allocated on the stack. */ #include <iostream> #include <string> using namespace std; class Fred { public: Fred(string s, int a) { m_val = a; m_lab = s; } int val() { return m_val; } string lab() { return m_lab; } Fred combine(Fred other) { return Fred(lab()+","+other.lab(), val()+other.val()); } private: int m_val; string m_lab; }; void doer(Fred f, int amt) { // Space for f and amt was allocated on the stack when the function was // called, and the declaration below allocates space there for newf. Fred newf("bigger " + f.lab(), amt + f.val()); cout << newf.lab() << ": " << newf.val() << endl; // At the closing }, the space for newf, amt and f on the stack is // released. } int main() { // These declarations allocate space for f1 and f2 on the stack. Fred f1("skunk", 41); Fred f2("wombat", 33); cout << f1.lab() << ": " << f1.val() << endl; cout << f2.lab() << ": " << f2.val() << endl; // Now, space for f3 is allocated. Fred f3 = f1.combine(f2); cout << f3.lab() << ": " << f3.val() << endl; // This makes a copy of f3 in space allocated on the stack, // as well as allocating stack space for the 7. doer(f3, 7); // At the closing }, all stack space allocated since the // opening { is freed. That would be f1, f2 and f3. }