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 4058272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:03:32+00:00 2026-05-20T15:03:32+00:00

recently I have come across these two ways of creating an object in a

  • 0

recently I have come across these two ways of creating an object in a specific place in memory:

1.

void* mem = malloc(sizeof(T));
T* obj = new(mem) T();

2.

T* obj = (T*)malloc(sizeof(T));
*obj = T();

The second way is a bit shorter…are there other differences?
Regards
Mateusz

  • 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-05-20T15:03:33+00:00Added an answer on May 20, 2026 at 3:03 pm

    The second way is wrong, the first is right.

    You’re invoking the assignment operator on a T instance containg garbage data. The assignment operator expects the instance to have been initialized correctly – It could e.g. delete member variables before assigning, which would cause all sorts of funny crashes. See e.g:

    struct Foo {
      std::string * Data;
      Foo() : Data(0) {}
      Foo(Foo const & R)  { Data = new std::string(*R.Data); }
      ~Foo() { delete Data; }
      Foo & operator=(Foo const & R) {
        delete Data;
        Data = new std::string(*R.Data);
        return *this;
      }
    
    };
    

    The first way will ensure Foo::Foo() is called – thus properly initializing Data. The second way will lead to a delete Data; where Data points to some random location in memory.

    EDIT:

    You can test this the following way:

    void* mem = malloc(sizeof(Foo));
    memset(mem, 0xCC, sizeof(Foo)); // malloc doesn't guarantee 0-init
    Foo* obj = new(mem) Foo();
    

    And:

    Foo * obj = (Foo*)malloc(sizeof(Foo));
    memset(obj, 0xCC, sizeof(Foo)); // malloc doesn't guarantee 0-init
    *obj = Foo(); // Crash
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started learning c# and have come across an issue. I'm not
I have recently come across an interesting question on strings. Suppose you are given
My co-worker and I have come across this warning message a couple times recently.
I have never come across this issue but most recently I noticed that a
I have recently come across possibly what is the most silly implementation of application
Recently I have come across a curious pattern in some code. We know that
I have recently come across numeric literals such as 10! and 50# in Visual
I've been studying Python 3 recently and I have come across a conundrum: I
I have recently come across a very simple Typed DataTable (without using a .XSD)(I've
Ive come across a nice problem recently, I have a Map LinkedHashMap<String, List<MyCustomObject>> I

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.