Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:42:31+00:00 2026-06-03T23:42:31+00:00

I have a question regarding placement new syntax in C++. Are the following two

  • 0

I have a question regarding placement new syntax in C++. Are the following two code snippets functionally equivalent and can be used interchangeably (I am not implying that the second should be used, when the first one is suitable)?

#1

T* myObj = new T();
// Do something with myObj
delete myObj;

#2

char* mem = new char[sizeof(T)];
T* myObj = new (mem) T();
// Do something with myObj
myObj->~T();
delete[] mem;

Is there something I should be especially careful of, when I am using the placement new syntax like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:42:33+00:00Added an answer on June 3, 2026 at 11:42 pm

    They are not equivalent, because they have different behaviour if the constructor or the destructor of T throws.

    new T() will free any memory that has been allocated before letting the exception propagate any further. char* mem = new char[sizeof(T)]; T* myObj = new (mem) T(); will not (and unless you explicitly do something to ensure that it gets freed you will have a leak). Similarly, delete myObj will always deallocate memory, regardless of whether ~T() throws.

    An exact equivalent for T* myObj = new T();/*other code*/delete myObj; would be something like:

    //When using new/delete, T::operator new/delete
    //will be used if it exists.
    //I don't know how do emulate this in
    //a generic way, so this code just uses
    //the global versions of operator new and delete.
    void *mem = ::operator new(sizeof(T));
    T* myObj;
    try {
        myObj = new (mem) T();
    }
    catch(...) {
        ::operator delete(mem);
        throw;
    }
    /*other code*/
    try {
        myObj->~T();
        ::operator delete(mem);
    }
    catch(...) {
        //yes there are a lot of duplicate ::operator deletes
        //This is what I get for not using RAII ):
        ::operator delete(mem);
        throw;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question regarding the SQLAlchemy. How can I add into my mapped class
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding OpenGL glBufferData() method.Can I pass the vertex data in
I have a question regarding threading in Java. In my code, ... client.doSth(); //
Have a question regarding URL and jQuery. Can I specify URL to tell jQuery
I have a question regarding how to use the formula property to be used
I have a question regarding the execution of the following program. #include<stdio.h> int main(void)
I have a question regarding this code. I'm a beginner and enjoy learning C#.
I have a question regarding the following method calls: var ctl1 = this.FindControlRecursively(SomeField) as
I have question regarding the use of function parameters. In the past I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.