Placement New

Placement New - Placement New/Delete In C+, operators...

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

View Full Document Right Arrow Icon
Placement New/Delete In C++, operators new/delete mostly replace the use of malloc() and free() in C. For example: class A { public: A(); ~A(); }; A* p = new A; ... delete p; allocates storage for an A object and arranges for its constructor to be called, later followed by invocation of the destructor and freeing of the storage. You can use the standard new/delete functions in the library, or define your own globally and/or on a per-class basis. There's a variation on new/delete worth mentioning. It's possible to supply additional parameters to a new call, for example: A* p = new (a, b) A; where a and b are arbitrary expressions; this is known as "placement new". For example, suppose that you have an object instance of a specialized class named Alloc that you want to pass to the new operator, so that new can control allocation according to the state of this object (that is, a specialized storage allocator): class Alloc {/* stuff */}; Alloc allocator; ... class A {/* stuff */};
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.

Page1 / 2

Placement New - Placement New/Delete In C+, operators...

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