Chapter13 -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Viewing Hints  ] [  Exercise Solutions  ] [  Volume 2  ] [  Free Newsletter  ]  Seminars  ] [  Seminars on CD ROM  ] [  Consulting  ]  Thinking in C++, 2nd ed. Volume 1 ©2000 by Bruce Eckel Previous Chapter  ] [  Table of Contents  ] [  Index  ] [  Next Chapter  ]  13: Dynamic Object Creation Sometimes you know the exact quantity, type, and lifetime of the objects in your  program. But not always. How many planes will an air-traffic system need to handle? How many shapes will a CAD system  use? How many nodes will there be in a network? To solve the general programming problem, it’s essential that you be able to create and destroy  objects at runtime. Of course, C has always provided the  dynamic memory allocation  functions  malloc( )  and  free( )  (along with variants of  malloc( ) ) that allocate storage from the  heap  (also  called the  free store ) at runtime. However, this simply won’t work in C++. The constructor doesn’t allow you to hand it the address  of the memory to initialize, and for good reason. If you could do that, you might: 1. Forget. Then guaranteed initialization of objects in C++ wouldn’t be guaranteed. 2. Accidentally do something to the object before you initialize it, expecting the right thing to  happen. 3. Hand it the wrong-sized object. And of course, even if you did everything correctly, anyone who modifies your program is prone to  the same errors. Improper initialization is responsible for a large portion of programming problems,  so it’s especially important to guarantee constructor calls for objects created on the heap. So how does C++ guarantee proper initialization and cleanup, but allow you to create objects  dynamically on the heap? The answer is by bringing dynamic object creation into the core of the language.  malloc( )  and  free( )  are library functions, and thus outside the control of the compiler. However, if you have an  operator  to perform the combined act of dynamic storage allocation and initialization and another  operator to perform the combined act of cleanup and releasing storage, the compiler can still  guarantee that constructors and destructors will be called for all objects.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In this chapter, you’ll learn how C++’s  new  and  delete  elegantly solve this problem by safely  creating objects on the heap. Object creation When a C++ object is created, two events occur: 1. Storage is allocated for the object. 2. The constructor is called to initialize that storage.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/10/2011 for the course IT 331 taught by Professor Nevermind during the Spring '11 term at King Abdulaziz University.

Page1 / 26

Chapter13 -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online