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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:53:43+00:00 2026-05-10T13:53:43+00:00

Is it possible to actually make use of placement new in portable code when

  • 0

Is it possible to actually make use of placement new in portable code when using it for arrays?

It appears that the pointer you get back from new[] is not always the same as the address you pass in (5.3.4, note 12 in the standard seems to confirm that this is correct), but I don’t see how you can allocate a buffer for the array to go in if this is the case.

The following example shows the problem. Compiled with Visual Studio, this example results in memory corruption:

#include <new> #include <stdio.h>  class A {     public:      A() : data(0) {}     virtual ~A() {}     int data; };  int main() {     const int NUMELEMENTS=20;      char *pBuffer = new char[NUMELEMENTS*sizeof(A)];     A *pA = new(pBuffer) A[NUMELEMENTS];      // With VC++, pA will be four bytes higher than pBuffer     printf('Buffer address: %x, Array address: %x\n', pBuffer, pA);      // Debug runtime will assert here due to heap corruption     delete[] pBuffer;      return 0; } 

Looking at the memory, the compiler seems to be using the first four bytes of the buffer to store a count of the number of items in it. This means that because the buffer is only sizeof(A)*NUMELEMENTS big, the last element in the array is written into unallocated heap.

So the question is can you find out how much additional overhead your implementation wants in order to use placement new[] safely? Ideally, I need a technique that’s portable between different compilers. Note that, at least in VC’s case, the overhead seems to differ for different classes. For instance, if I remove the virtual destructor in the example, the address returned from new[] is the same as the address I pass in.

  • 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. 2026-05-10T13:53:43+00:00Added an answer on May 10, 2026 at 1:53 pm

    Personally I’d go with the option of not using placement new on the array and instead use placement new on each item in the array individually. For example:

    int main(int argc, char* argv[]) {   const int NUMELEMENTS=20;    char *pBuffer = new char[NUMELEMENTS*sizeof(A)];   A *pA = (A*)pBuffer;    for(int i = 0; i < NUMELEMENTS; ++i)   {     pA[i] = new (pA + i) A();   }    printf('Buffer address: %x, Array address: %x\n', pBuffer, pA);    // dont forget to destroy!   for(int i = 0; i < NUMELEMENTS; ++i)   {     pA[i].~A();   }        delete[] pBuffer;    return 0; } 

    Regardless of the method you use, make sure you manually destroy each of those items in the array before you delete pBuffer, as you could end up with leaks 😉

    Note: I haven’t compiled this, but I think it should work (I’m on a machine that doesn’t have a C++ compiler installed). It still indicates the point 🙂 Hope it helps in some way!


    Edit:

    The reason it needs to keep track of the number of elements is so that it can iterate through them when you call delete on the array and make sure the destructors are called on each of the objects. If it doesn’t know how many there are it wouldn’t be able to do this.

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

Sidebar

Related Questions

Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do you send email from a Java app using Gmail? How
I want to make '==' operator use approximate comparison in my program: float values
In C++ is it possible to use another base class to provide the implementation
Just wondering if this is possible. What I would actually like to do is
I am developing an application using the wrong tools. I don't wish to get
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on

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.