O'Reilly logo

Navigating C++ and Object-Oriented Design by Gail Anderson, Paul Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.5. Static Objects

Objects that you declare inside functions and blocks reside in a function's stack frame. The following code, for instance, creates a local Fifo object inside a function.

void sub() {
   Fifo f(100);               // local Fifo in stack frame
   . . .
}

The compiler generates code to allocate memory for f in sub()'s stack frame before calling the Fifo constructor. Likewise, the compiler calls the Fifo destructor before releasing memory from sub()'s stack frame when we return from sub(). Thus, local objects use stack frame memory, and the compiler calls constructors and destructors automatically.

To place objects into free store instead of a function's stack frame, we use operators new and delete.

 void sub() { . . . Fifo *pf = new Fifo(100); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required