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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:03:59+00:00 2026-05-22T12:03:59+00:00

If I am not modifying any static variable inside the argument constructor, is below

  • 0

If I am not modifying any static variable inside the argument constructor, is below the proper way to simulate new T[N] (x,y); (array new with arguments) ?

template<typename T>
void* operator new [] (size_t size, const T &value)
{
  T* p = (T*) malloc(size);
  for(int i = size / sizeof(T) - 1; i >= 0; i--)
    memcpy(p + i, &value, sizeof(T));
  return p;
}

Usage will be,

struct A
{
  A () {}  // default
  A (int i, int j) {} // with arguments
};

int main ()
{
  A *p = new(A(1,2)) A[10];  // instead of new A[10](1,2)
}
  • 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-22T12:04:00+00:00Added an answer on May 22, 2026 at 12:04 pm

    This isn’t OK. You are copying objects into uninitialised memory without invoking proper copy semantics.

    As long as you’re only working with PODs, this is fine. However, when working with objects that are not PODs (such as your A) you need to take precautions.

    Apart from that, operator new cannot be used in this way. As Alexandre has pointed out in the comments, the array won’t be initialised properly since C++ will call constructors for all elements after having called your operator new, thus overriding the values:

    #include <cstdlib>
    #include <iostream>
    
    template<typename T>
    void* operator new [] (size_t size, T value) {
        T* p = (T*) std::malloc(size);
        for(int i = size / sizeof(T) - 1; i >= 0; i--)
            new(p + i) T(value);
        return p;
    }
    
    struct A {
        int x;
        A(int x) : x(x) { std::cout << "int ctor\n"; }
        A() : x(0) { std::cout << "default ctor\n"; }
        A(const A& other) : x(other.x) { std::cout << "copy ctor\n"; }
    };
    
    int main() {
        A *p = new(A(42)) A[2];
        for (unsigned i = 0; i < 2; ++i)
            std::cout << p[i].x << std::endl;
    }
    

    This yields:

    int ctor
    copy ctor
    copy ctor
    default ctor
    default ctor
    0
    0
    

    … not the desired outcome.

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

Sidebar

Related Questions

I'm working on modifying some existing code for a payment gateway and I'm not
My understanding is any method which does not modify state of the constaining class
I'm interested in being able to instantiate multiple testfixtures with constructor arguments passed to
In Java, I have 2 threads that are both accessing (not modifying) the same
I'm new to JQuery. I'm not looking necessarily for code -- just someone to
I'm trying to get a regex that matches: (It should not match any other
Not very technical, but... I have to implement a bad words filter in a
Not sure if this is possible or if I'm expressing correctly what I'm looking
(Not related to versioning the database schema) Applications that interfaces with databases often have
Not really getting the point of the map function. Can anyone explain with examples

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.