Allocating Arrays of Objects
You can also create arrays of objects in dynamic memory using the new operator. You do this by providing the number of items you want created in a pair of square brackets. The following code allocates memory for two integers:
int *p = new int[2];
p[0] = 1;
*(p + 1) = 2;
for (int i = 0; i < 2; ++i) cout << p[i] << endl;
delete [] p;
The operator returns a pointer to the type allocated, and you can use pointer arithmetic or array indexing to access the memory. You cannot initialize the memory in the new statement; you have to do that after creating the buffer. When you use new to create a buffer for more than one object, you must use the appropriate version of the delete operator: the [] is used to indicate that more than one item is deleted and the destructor for each object will be called. It is important that you always use the right version of delete appropriate to the version of new used to create the pointer.
Custom types can define their own operator new and operator delete for individual objects, as well as operator new[] and operator delete[] for arrays of objects. The custom type author can use these to use custom memory allocation schemes for their objects.