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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:50:22+00:00 2026-06-12T04:50:22+00:00

In order to do some custom memory tracking (leak prevention, corruption detection), I’m having

  • 0

In order to do some custom memory tracking (leak prevention, corruption detection), I’m having to use placement new to create C++ objects, which works fine – but I’m struggling to figure out how I can pass arguments to the constructor, since it’s called from a macro (so the file + line can be provided automatically).

The function:

template <typename T>
T*
cpp_new(
    const char *file,
    size_t line
)
{
    T   *n = (T*)tracked_allocate(&memory_context, sizeof(T), file, line);

    if ( n )
    {
        construct(n);
    }
    else
    {
        throw std::bad_alloc();
    }

    return n;
}

This is called via the macro:

#define new_object(type)    cpp_new<type>(__FILE__, __LINE__)

Placemented new:

template <typename T>
void
construct(
    T *obj
)
{
    obj = new (obj) T;
}

The va_list macros would cover the expansion for the variable number of arguments, only I don’t want to supply the number of arguments the constructor has, removing va_arg(), and can’t use va_start(), since it expects a format.

This went a little over my head: http://www.drdobbs.com/cpp/calling-constructors-with-placement-new/232901023?pgno=2

Is there any way I can use __VA_ARGS__ from new_object and pass them into the construct function? Each object only has one constructor, but there are many different types of objects taking different parameters, so I want to remove as much manual maintenance as possible.

Or is there just a better way in general of doing what I’m attempting!

  • 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-12T04:50:23+00:00Added an answer on June 12, 2026 at 4:50 am

    You should not deal with the construct-if-allocation-succeeded. That’s the job of a new-expression. It does that correctly, as a transaction-like operation: either all succeeds, or cleanup is performed before the exception is propagated.

    So, relieved of that responsibility, the job of your macro is to do that which only macros can do, namely picking up the filename and line.

    Those items can/should be passed to allocator function, which then technically is a “placement new”, although here it will not construct in-place: it’s a placement new merely because it has extra, user-defined arguments, like this:

    bool hopefully( bool const c ) { return c; }
    
    template< class X >
    bool throw_( X const& x ) { throw x; }
    
    void* operator new( size_t const size, char const* const filename, int const linenum )
    {
        void* const p = tracked_allocate( &memory_context, size, filename, linenum );
        hopefully( p != 0 )
            || throw_( std::bad_alloc() )
        return p;
    }
    

    You need to define a corresponding placement deallocation function, or else a new expression will fail to deallocate when a constructor throws:

    void operator delete( void* const p )
    {
        // Your custom deallocation.
    }
    
    void operator delete( void* const p, char const*, int )
    {
        ::operator delete( p );
    }
    

    Now your macro just needs to provide the relevant placement arguments, like this:

    #define SOURCE_LINE_INFO __FILE__, __LINE__
    

    Then you can just say, like,

    new (SOURCE_LINE_INFO) MyType( arg1, arg2, arg3 )
    

    For a more reusable solution consider defining a struct to hold the filename and line number. Then the macro reduces to constructing an instance of that type, and can be used more generally. In particular, it can then be used for logging calls.

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

Sidebar

Related Questions

I have 2 custom objects: 'Package' and 'Order'. 'Order' includes 'Package' field and some
I started to use ggplot in order to generate some graphs. I am using
I have a custom view subclassed from GridView that I use in order to
I want to use multiset to count some custom defined keys. The keys are
I want to create my custom command and to hook it up to some
Often I could use some tools to statically analyze my code in order to
I have added some custom variables to customer and I want to use them
I'm having some trouble creating some custom web services in Magento. I'm trying to
So I have a class is something like this public class Order { //some
I have next tables Order , Transaction , Payment . Class Order has some

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.