原创 Deallocating objects vs. deallocating storage (Part 1)

2011-3-10 11:10 1644 8 8 分类: 消费电子

Here's how delete-expressions interact with destructors and deallocation functions in C++.


I wrote a couple of articles on the distinction between allocating objects and allocating raw storage. In essence, when a program allocates an object, it not only allocates storage for the object but also initializes that storage with a value appropriate for the type of object that will occupy that storage. When a program just allocates storage, it leaves that storage uninitialized. Calling storage "raw" is a common way to emphasize that it's not initialized.


The distinction between storage and objects is readily apparent in the behavioral difference between the Standard C malloc function and a C++ new-expression. Specifically, a C (or C++) expression such as:


pw = (widget *)malloc(sizeof(widget));


allocates raw storage. Indeed, the malloc call allocates storage that's big enough and suitably aligned to hold an object of type widget, but it leaves that storage uninitialized. In contrast, a C++ new-expression as in:


pw = new widget ();


creates a widget object with a coherent initial value.


The distinction between storage and objects carries over when allocating array objects. A call to malloc as in:


pw = (widget *)malloc(10 * sizeof(widget));


allocates raw storage for an array of 10 widgets. By contrast, the C++ array new-expression in:
pw = new widget [10];


allocates an array of 10 properly initialized widgets.


Each new-expression is conceptually, if not actually, a two-step process: (1) allocate storage for an object, and (2) initialize it. For objects of class types, initializing an object usually involves calling a constructor. An array new-expression is also a two-step process, but the second step is a loop that initializes every array element.


It should come as no surprise that the distinction between objects and raw storage also comes into play when you deallocate them. Just as allocating an object in C++ may involve calling a constructor, deallocating an object may involve calling a destructor.


This month I'll explain how delete-expressions interact with destructors and deallocation functions in C++. I'll also explain how C programmers can employ a style of memory deallocation that mimics the behavior of C++ delete-expressions.


Delete-expressions and destructors
In C++, a destructor is a special class member function that "destroys" objects of that class type. Generally, to destroy an object means to deallocate or release the resources used by that object. A destructor's function name is always the same as its class name, but starting with a ~ (tilde, or "squiggle" if you prefer) as in:



class widget
{
public:
widget(); // a constructor
~widget(); // a destructor
// ...
};


Just as constructors provide guaranteed initialization for class objects, destructors provide guaranteed destruction. Just as you don't write explicit calls to constructors—compilers generate them for you—you don't write explicit destructor calls either. (Actually, you can write explicit destructor calls, but it's something you should avoid doing if possible.) Whenever you define an object with a class type, the compiler automatically plants calls to the object's destructor at the point(s) in the program where that object's lifetime ends—typically at function return statements and at the end (the closing brace) of function bodies and statement blocks.


For guaranteed destruction to really be guaranteed, the compiler must generate a call to a destructor at every point where the program destroys an object, including in delete-expressions. Thus, if pw is a pointer to a single widget, a delete-expression such as:



delete pw;


doesn't just deallocate storage for that widget; it applies widget's destructor to the object addressed by pw to release any resources that the widget may have in its possession.


Default initialization for scalar types, such as int and double, does nothing. When you dynamically allocate an object of scalar type, as in:



int *pi = new int;


the allocated object remains uninitialized. Default destruction for scalar types does nothing as well. An object of scalar type consumes no resources beyond its own storage, so there's nothing for its "destructor" to do. Thus, a delete-expression such as:



delete pi;


just deallocates the storage for the int addressed by pi.


As I explained in previous blog Destroying everything in path, a destructor doesn't deallocate the storage for an object.1 It releases the resources managed by the object, some of which may be storage. The storage for the object being destroyed will be deallocated by some other runtime agent of the program. For example, for a mythical widget object declared local to a function, the destructor for that widget executes just before the function returns. The destructor releases the widget's resources, and then the runtime system deallocates the widget's storage by popping it off the stack along with the rest of the function's stack frame.


Just as the array new-expression in:



pw = new widget [10];


applies the default widget constructor to each element in the storage allocated for the array, an array delete-expression such as:


delete [ ] pw;


applies the widget destructor to each array element. Whereas an array new-expression applies the constructors to the array element in ascending order by subscript starting at the 0th element, an array delete-expression applies the destructors in the reverse order.


Delete-expressions and operator delete
Just as a new-expression allocates memory by calling a function named operator new (rather than malloc), a delete-expression deallocates memory by calling a function named operator delete (rather than free). Each C++ environment provides a default implementation for a global operator delete, declared in the standard header as:



void operator delete(void *p) throw ();


The empty exception specification:



throw ()


at the end of the function heading indicates that operator delete isn't supposed to allow any exceptions to propagate from the function. That is, operator delete may throw exceptions of various types, but it will catch those other exceptions before they can escape to the calling environment. However, as Scott Meyers explains, an empty exception specification doesn't really guarantee that no exceptions will propagate.2


If pw is a pointer to an object of class type widget, a delete-expression such as:



delete pw;


translates more-or-less into something like:



if (pw != NULL)
{
pw->~widget();
operator delete(pw);
}

 

 

[continued at Deallocating objects vs. deallocating storage (Part 2)]

 

文章评论0条评论)

登录后参与讨论
我要评论
0
8
关闭 站长推荐上一条 /2 下一条