Memory Management in RWDBTBuffer
Since we allocated our own memory for data buffers in the previous section, we are now responsible for deallocating it. Of course, we can always use delete to deallocate the memory directly. Alternatively, however, we can let RWDBTBuffer allocate and deallocate storage automatically by using a constructor that doesn’t take a pointer to our data. Allowing RWDBTBuffer to manage its own memory frees us from explicitly managing memory, and simplifies our code in many cases.
To take advantage of automatic memory management, we simply construct the RWDBTBuffer, specifying only the number of elements to allocate. When we need access to the data, we use the overloaded operator[], as shown in this example:
 
{
// Allocates 100 RWDateTimes
RWDBTBuffer<RWDateTime> someDates(100);
 
// Change element index 20:
someDates[20] = RWDateTime(28,7,1979);
 
// Now get the date using RWDBTBuffer<RWDateTime>::operator[]:
cout << “A date: “ << someDates[20];
 
// When we leave this block, the 100 RWDateTimes are
// automatically deallocated.
}
In this example, the destructor of RWDBTBuffer automatically destroys the 100 RWDateTimes allocated by the constructor. Had we passed in a pointer to our own array in the RWDBTBuffer constructor, it would not have been deallocated, because it is not owned by the RWDBTBuffer instance. RWDBTBuffer deletes only the data it owns.