-
Book Overview & Buying
-
Table Of Contents
The C++ Workshop
By :
In the previous chapter, we learned how to create and delete dynamic variables and dynamic arrays. We also learned at least seven ways to make a mistake using dynamic variables that will crash your program. Obviously, these powerful tools require some discipline in order to use correctly. In this chapter, we describe ways to manage dynamic variables that reduce the chances of making a mistake when working with them, beginning with the concepts of lifetime and ownership.
Lifetime and ownership are key concepts that experienced developers use to tame the complexity of dynamic variables so that memory leaks do not occur. The concepts of lifetime and ownership of dynamic variables are not fully expressed by C++ syntax. They are things that the developer must manage.
The result of each new-expression must be assigned to a pointer variable, or else the new dynamic variable becomes inaccessible. This pointer variable may be said to own the dynamic variable when it...