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

  • SEARCH
  • Home
  • 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 8284131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:58:57+00:00 2026-06-08T10:58:57+00:00

From reading this post , it is clear that placement news in c++ are

  • 0

From reading this post, it is clear that placement news in c++ are used to call a class constructor on a pre-allocated memory location.

In the case that the memory is already initialized, is a placement new or a reinterpret_cast more appropriate?

For example, let’s say I read a raw stream of bytes representing a framed message from a TCP socket. I put this stream into a framesync and retrieve a buffer of a known size that represents my class, which I’ll call Message. I know of two ways to proceed.

  1. Create a constructor that takes a flag telling the class not to initialize. Do a placement new on the buffer passing the “don’t initialize” flag.

    Message::Message( bool initialize ) 
    {
        //
        // Initialize if requested
        //
        if( initialize )
        {
            Reset( );
        }
    }
    
    void Message::Reset( void )
    {
       m_member1 = 1;
       m_member2 = 2;
    }
    
    Message* message = new ( buffer ) Message( false );
    
  2. Use a reinterpret_cast

    Message* message = reinterpret_cast< Message* > ( buffer ); 
    

I believe that both of these will produce an identical result. Is one preferred over the other as more correct, more OO, safer, easier to read, or better style?

  • 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-08T10:58:59+00:00Added an answer on June 8, 2026 at 10:58 am

    The only meaningful rule is this:

    If an instance of some type T has already been constructed at address a, then reinterpret_cast<T*>(a) to get a pointer to the object that already exists.

    If an instance of some type T has not yet been constructed at address a, then use placement new to construct an instance of type T at addres a.

    They are completely different operations.

    The question you need to ask is very, very simple: “does the object already exist?”
    If yes, you can access it (via a cast). If no, then you need to construct it (via placement new)

    The two operations have nothing to do with each others.

    It’s not a question of which one you should prefer, because they do different things. You should prefer the one which does what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should be an easy one. I thought, from reading this blog post that I
After reading this post i realized that i cannot pass an anonymous type as
As I understand from reading this post about the new invokedynamic bytecode instruction in
After reading this post (recommended reading) about not using HTML5Shiv direct from source like
The title is probably not accurate but I hope that reading this post you
I'm trying to make a custom view in Django admin. I'm reading from this
After reading this article, it makes sense to rebase to gather changes from the
I am reading this book Beginning Java™ EE 6 Platform with GlassFish™ 3: From
Firs of all thanks for reading this. I'm having trouble updating the progress from
Firstly, sorry for my English (I'm from China). By reading this article( how to

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.