delete (pi);

Interestingly, C++ terms these functions replaceable. That means if you have sufficient expertise and desire, you can supply replacement functions for new and delete and tailor them to meet your specific requirements. One option, for instance, is to define replacement functions with class scope so that they can be tailored to fit the allocation needs of a particular class. Your code would use the new operator as usual, but the new operator would call upon the replacement new() function.

The Placement new Operator

Normally, the new operator has the responsibility of finding in the heap a block of memory that is large enough to handle the amount of memory you request. The new operator has a variation, called placement new, that allows you to specify the location to be used. A programmer might use this feature to set up his or her own memory-management procedures or to deal with hardware that is accessed via a particular address or to construct objects in a particular memory location.

To use the placement new feature, you first include the new header file, which provides a prototype for this version of new. Then you use new with an argument that provides the intended address. Aside from this argument, the syntax is the same as for regular new. In particular, you can use placement new either without or with brackets. The following code fragment shows the syntax for using these four forms of new:

#include

struct chaff

{

    char dross[20];

    int slag;

};

char buffer1[50];

char buffer2[500];

int main()

{

    chaff *p1, *p2;

    int *p3, *p4;

// first, the regular forms of new

    p1 = new chaff;              // place structure in heap

    p3 = new int[20];            // place int array in heap

// now, the two forms of placement new

    p2 = new (buffer1) chaff;    // place structure in buffer1

    p4 = new (buffer2) int[20];  // place int array in buffer2

...

For simplicity, this example uses two static arrays to provide memory space for placement new. So this code allocates space for a chaff structure in buffer1 and space for an array of 20 ints in buffer2.

Now that you’ve made your acquaintance with placement new, let’s look at a sample program. Listing 9.10 uses both new and placement new to create dynamically allocated arrays. This program illustrates some important differences between new and placement new that we’ll discuss after seeing the output.

Listing 9.10. newplace.cpp

// newplace.cpp -- using placement new

#include

#include          // for placement new

const int BUF = 512;

const int N = 5;

char buffer[BUF];      // chunk of memory

int main()

{

    using namespace std;

    double *pd1, *pd2;

    int i;

    cout << "Calling new and placement new:\n";

    pd1 = new double[N];           // use heap

    pd2 = new (buffer) double[N];  // use buffer array

    for (i = 0; i < N; i++)

        pd2[i] = pd1[i] = 1000 + 20.0 * i;

    cout << "Memory addresses:\n" << "  heap: " << pd1

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги