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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:10:41+00:00 2026-06-13T20:10:41+00:00

My question is about a code snanpshot from Scott Meyer’s Book More Effective C++

  • 0

My question is about a code snanpshot from Scott Meyer’s Book “More Effective C++ 35 new ways …”

the code (parameter names are changed)

void * memory = operator new[] (10*sizeOf(MyClass));
MyClass * myArray = static_cast<MyClass*>(memory);
for(int i= 0; i<10; i++)
{
new (&myArray[i]) MyClass(params);
}

I am not familiar with this syntax. Even the operator new [] and the new (&myArray[i]) … Is there any resource that I can read detailed about that syntax, how they are working.

  • 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-13T20:10:42+00:00Added an answer on June 13, 2026 at 8:10 pm

    To understand this, you first need to understand how plain and simple new works. The usual syntax of a new expression is something like new T. When you use a new expression, the following occurs:

    1. First, it calls an allocation function to obtain storage for the object. The allocation function simply has to return a pointer to some allocated storage large enough to fit the requested object. It does no more than this. It doesn’t initialize the object.

    2. Next, the object is initialized in the allocated space.

    3. A pointer to the allocated space (and now initialized object) is returned.

    In the case of new T, the allocation function is named operator new. When allocating an array, such as new T[5], the allocation function is named operator new[]. Default definitions of these functions are provided in the global namespace. They each take a single argument of type std::size_t which is the number of bytes required. So when you do new T, the corresponding call is to operator new(sizeof(T)), whereas for new T[5], operator new[](sizeof(T)*5) gets called.

    It is, however, possible to pass more arguments to the allocation function. This is known as the placement new syntax. To pass more arguments, you use syntax like so: new (some, arguments, 3) T, which will call operator new(sizeof(T), some, arguments, 3). The list of arguments goes in parentheses between new and the type.

    In addition to the simple operator new(std::size_t) and its array counterpart that are both provided by the implementation, there are also default definitions that take an extra argument of type void*. That is, they take a pointer to an object. These functions don’t actually allocate any space and simply return the pointer you gave them. So when you do new (some_pointer) T, it first calls operator new(sizeof(T), some_pointer) which just returns some_pointer back again and then it initializes the object in that space. This gives you a way to initialize an object in some already allocated space.

    So now we have these four pre-defined allocation functions (in fact, there are a few others and you’re free to define your own too):

    // Normal allocation functions that allocate space of the given size
    operator new(std::size_t)
    operator new[](std::size_t)
    // Placement allocation functions that just return the pointer they're given
    operator new(std::size_t, void*)
    operator new[](std::size_t, void*)
    

    So lets take a look at the code snippet you provided:

    void * memory = operator new[] (10*sizeOf(MyClass));
    MyClass * myArray = static_cast<MyClass*>(memory);
    for(int i= 0; i<10; i++)
    {
      new (&myArray[i]) MyClass(params);
    }
    

    In the first line, we are calling operator new[] directly to allocate some storage. How much storage? Well, enough for 10 objects of type MyClass. This function returns a pointer to that allocated storage and you store it in memory.

    After this, the void* is cast to a MyClass* to allow you to index the allocated storage in blocks of size sizeof(MyClass), i.e. myArray[0] will now point to the first MyClass and myArray[1] to the second.

    Now we loop through that array, calling placement new with the address of each uninitialized MyClass-sized bit of allocated storage. In the first iteration, for example, this will call allocation function operator new(sizeof(MyClass), &myArray[0]) which, as we saw before, does nothing but return the pointer you’ve given it. The new expression will complete by initializing a MyClass object in this space and returning the pointer to it.

    So in summary, the code allocates some storage to fit 10 MyClass objects (but doesn’t initialize them), then loops through each MyClass-sized space in that storage, initializing an object in each of them.

    This demonstrates how you can initialize objects in already pre-allocated storage. You can reuse the same storage over and over with newly initialized objects.

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

Sidebar

Related Questions

I had a general question about extracting code from a visual studio web test.
I have a simple question about my code. I'm quite new to java and
My question is more so a question about code efficiency and simplicity than it
So I just have posted a question about this code (which was answered): $(document).ready(Main);
i have what i hope is a quick question about some code i am
I have a question about the following code in TCL/EXPECT: expect { timeout {
I recently asked a question about formatting JavaScript code in Vim. And I've also
I have a question about the following code : #include <iostream> #include <ctime> int
I have a question about the following C code: void my_function() { int i1;
I have read many question about improving the performance of C++ and C code

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.