Skip to Content
Navigating C++ and Object-Oriented Design
book

Navigating C++ and Object-Oriented Design

by Paul Anderson, Gail Anderson
October 1997
Intermediate to advanced
800 pages
20h 48m
English
Pearson
Content preview from Navigating C++ and Object-Oriented Design

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); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exploring C++20: The Programmer's Introduction to C++

Exploring C++20: The Programmer's Introduction to C++

Ray Lischner

Publisher Resources

ISBN: 0135327482Purchase book