Constructor Functions  «Prev  Next»
Lesson 2 Memory allocation for variables
Objective Review concepts of memory allocation/initialization of variables.

Memory Allocation for Variables

An object's constructor is automatically called when the object is created. This means that it is called when the object's declaration is executed. If you are accustomed to thinking of a declaration statement as being passive, this is not the case for C++. In C++, a declaration statement is a statement that is executed. This distinction is not just academic. The code executed to construct an object may be quite significant. An object's constructor is called once for global or static local objects. For local objects, the constructor is called each time the object declaration is encountered.

C++ Allocators

Each container has defined for it an allocator. Allocators manage memory allocation for a container. The default allocator is an object of class allocator, but you can define your own allocators, if needed, for specialized applications. For most uses, the default allocator is sufficient.

C and C++ provide memory allocation and initialization of variables through declarations that are definitions.
For example, in

void foo()
{
int n = 5;
double z[10] = { 0.0 };
struct gizmo { int i, j; } w = { 3, 4 };
. . .
}

all the variables are created at block entry when foo() is invoked. Upon exit from foo(), deallocation occurs automatically.
Remember that an object is a class variable, which requires memory and some initial values. A class needs a mechanism to specify object creation and object destruction behavior so a client can use objects in a manner similar to native types. Classes use constructor and destructor member functions to manage class-defined objects.

Dynamic Memory Allocation

A common programming practice is to
  1. allocate dynamic memory,
  2. assign the address of that memory to a pointer,
  3. use the pointer to manipulate the memory and
  4. deallocate the memory with delete when the memory is no longer needed.
If an exception occurs after successful memory allocation but before the delete statement executes, a memory leak could occur.
The C++ standard provides class template auto_ptr in header file <memory> to deal with this situation.
An object of class auto_ptr maintains a pointer to dynamically allocated memory. When an auto_ptr object destructor is called (for example, when an auto_ptr object goes out of scope), it performs a delete operation on its pointer data member.
Class template auto_ptr provides overloaded operators * and -> so that an auto_ptr object can be used just as a regular pointer variable is. Figure 4.2.3 demonstrates an auto_ptr object that points to a dynamically allocated object of class Integer (Figs.Figure 4.2.1-Figure 4.2.2).
Integer class definition
Figure 4.2.1 Integer class definition

Member function definitions of class Integer
C++ class Integer
Figure 4.2.2: Member function definitions of class Integer.

Line 15 of Fig. 4.2.3 creates auto_ptr object ptrToInteger and initializes it with a pointer to a dynamically allocated Integer object that contains the value 7. Line 18 uses the auto_ptr overloaded -> operator to invoke function setInteger on the Integer object that ptrToInteger manages. Line 21 uses the auto_ptr overloaded * operator to dereference ptrToInteger, then uses the dot (.) operator to invoke function getInteger on the Integer object. Like a regular pointer, an auto_ptr’s -> and * overloaded operators can be used to access the object to which the auto_ptr points.

auto_ptr object manages. dynamically allocated memory.
Figure 4.2.3: auto_ptr object manages dynamically allocated memory.