Operators new - Operators new[] and delete[] The C+ library...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Operators new[] and delete[] The C++ library has long had operator new() and delete() for dynamic storage allocation. Note that with these there's a distinction made between the operators specified as keywords, as in: new A[10]; and the functions, for example: operator new(159); The former usage not only is responsible for allocating space, via operator new(), but also for arranging for constructors to be called for the individual objects in the array slots. So normally you will not use operator new() directly. More recently the functions operator new[]() and operator delete[]() have been added to the language. These are like operator new() and operator delete(), but are invoked when arrays are being allocated and deallocated. To see how this works, consider an example such as: #include <stddef.h> #include <stdio.h> class A { int x; public: A() {printf("A::A %lx\n", (unsigned long)this);} ~A() {printf("A::~A %lx\n", (unsigned long)this);} }; void* operator new[](size_t sz) { printf("allocated size = %lu\n", (unsigned long)sz);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University.

Page1 / 3

Operators new - Operators new[] and delete[] The C+ library...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online