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

  • Home
  • SEARCH
  • 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 8168287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:28:25+00:00 2026-06-06T20:28:25+00:00

Is it possible to pass some parameters to constructor of a class inside a

  • 0

Is it possible to pass some parameters to constructor of a class inside a constructor of another class using malloc? I can do that with new. I need to do the same thing with malloc:
(If it does not make sense, consider that I am using a custom allocator instead of malloc)

Class A_Class ... {
       public:
       B_Class *b;
...
     A_Class: ...
      { b = new B_Class (c1, c2, c3);
      } // end of constructor
}

Now with malloc:

Class A_Class ... {
       public:
       B_Class *b;
...
     A_Class: ...
      { b = (B_Class*) malloc (sizeof(*b)); 
        ??????????????? }
}
  • 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-06T20:28:27+00:00Added an answer on June 6, 2026 at 8:28 pm

    malloc allocates raw memory. There’s no point in trying to pass constructor arguments to it because it doesn’t call any constructors.

    If you have to work with raw memory, it is up to you to construct an object in previously allocated raw memory by using “placement new” syntax

    ...
    void *raw_b = malloc(sizeof *b);
    b = new(raw_b) B_Class(c1, c2, c3); // <- placement new 
    ...
    

    Numerically, the value of b will be the same as raw_b, i.e. it is possible to get by without an extra raw_b pointer. But I prefer to do it this way, with an intermediate void * pointer, to avoid ugly casts.

    Be careful when destroying such objects, of course. Custom allocation requires custom deletion. You can’t just delete your b in general case. A symmetrical custom deletion sequence would involve an explicit destructor call followed by raw memory deallocation

    b->~B_Class(); // <- explicit destructor call
    free(b);
    

    P.S. You might consider going a different route: overloading operator new/operator delete in your class instead of spelling out custom allocation explicitly every time you need to create an object.

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

Sidebar

Related Questions

I am currently using a number of query string parameters to pass some data
Is it possible to pass some variables to the called function, it tried it
Possible Duplicate: pass by reference not working I was going through some of the
I have that template class that uses a policy for it's output and another
I am pretty new to Wicket and i have some difficulties with using resource
While using reflection in C#, you're expected to pass an object[] of the parameters
I have a custom method that accepts two parameters. I am using this method
I'm trying to write a Node.js module, using C++, that wraps and exposes some
I have some class C and want to pass address of its instance and
Is it possible to pass a delegate to a WCF remote object from the

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.