Placement new

This is a discussion on Placement new within the C++ Programming forums, part of the General Programming Boards category; It has recently come to my attention that it is possible to use new and delete to allocate and release ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053

    Placement new

    It has recently come to my attention that it is possible to use new and delete to allocate and release memory without calling constructors and destructors and to use new to call an object's constructor for already allocated memory. Can somebody point me to a semi official resource explaining how this is done? I am also interested in how this relates to overloading new.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    To allocate memory without calling constructors, you call operator new directly (not through the new operator - don't confuse the two):
    Code:
    void *mem = operator new(size);
    This is pretty much equivalent to calling malloc(), except of course that
    1) new throws on allocation failure and
    2) new can be overloaded.

    Placement new is used to construct an object in pre-allocated memory:
    Code:
    Foo *pfoo = new (mem) Foo;
    For a semi-official resource, try Item 8 of Scott Meyers's More Effective C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Are there some idioms or patterns there calling/overwriting operator new is useful?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The STL allocators basically do this, or a variant of it. Memory pools do it. Boost.Variant is built on placement new.
    Overloading new is sometimes useful for special memory management strategies.

    All in all, though, the cases where you need either feature are special, found on a case-by-case basis and do not come up in regular design patterns.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by CornedBee View Post
    To allocate memory without calling constructors, you call operator new directly (not through the new operator - don't confuse the two):
    Code:
    void *mem = operator new(size);
    This is pretty much equivalent to calling malloc(), except of course that
    1) new throws on allocation failure and
    2) new can be overloaded.

    Placement new is used to construct an object in pre-allocated memory:
    Code:
    Foo *pfoo = new (mem) Foo;
    For a semi-official resource, try Item 8 of Scott Meyers's More Effective C++.
    I'll see if I can get that book at my local library.

    What about deleting memory that has already been destroyed?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use operator delete directly:
    Code:
    operator delete(mem);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    This thread wouldn't be complete without mentioning that there is no corresponding placement-delete.
    To destruct an object without freeing its memory, use a direct destructor call:
    Code:
    pFoo->~Foo();
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curly Brace Placement
    By sean in forum General Discussions
    Replies: 96
    Last Post: 06-16-2009, 10:08 PM
  2. Placement new?
    By Elysia in forum C++ Programming
    Replies: 9
    Last Post: 02-26-2008, 03:50 AM
  3. My Placement come true
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-07-2004, 06:07 AM
  4. Placement..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-01-2004, 09:21 PM
  5. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 05:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21