How to call a constructor explicitly if we want to allocate memory using malloc ?

Discussion in 'C++' started by shsingh, Oct 18, 2006.

  1. shsingh

    shsingh Guest

    I have a class A containing some map as data variables. I creat an
    object of class A on heap by allocatiing memory by using "malloc". This
    will return me the required memory but the object is not initialized
    properly as constructor same is not get called ( as per the behavior).

    How to call a constructor explicitly if we want to allocate memory
    using malloc ?
    shsingh, Oct 18, 2006
    1. Advertisements

  2. shsingh

    Larry Smith Guest

    Your create object on the heap with new, not malloc.

    A *pA = new A;

    You free the memory and object with delete.

    delete pA;
    Larry Smith, Oct 18, 2006
    1. Advertisements

  3. Use "placement new" (look it up).

    Victor Bazarov, Oct 18, 2006
  4. shsingh

    Bart Guest

    You can use placement new.

    buffer = malloc(/*...*/);
    object = new (buffer) MyClass(/*...*/);

    But later you have to call the destructor explicitly too:


    Bart, Oct 18, 2006
  5. shsingh

    Ron Natalie Guest

    You can't call a constructor. You can construct an object in
    memory you provide (from malloc or anyplace else) with
    placement new:

    #include <new>

    void* memory = malloc(sizeof T);
    T* tp = new (memory) T;
    Ron Natalie, Oct 18, 2006
  6. shsingh

    zouyongbin Guest

    Is that correct to construct the object on the memory allocated by
    malloc? Is that behavior defined in standard C++? I think that the
    memory should be allocated by operator new if you want to use placement

    Best Regards,
    zouyongbin, Oct 18, 2006
  7. shsingh

    Bart Guest

    No. Placement new was invented specifically so you can use your own
    memory allocator.

    The memory doesn't even have to be "allocated" at all. If you were to
    write an OS kernel in C++ you could just construct some object at
    whatever physical address you like without ever allocating any memory.

    Bart, Oct 18, 2006
  8. shsingh

    Kai-Uwe Bux Guest

    Can you call function? or is all that *you* can do writing an expression in
    your code and then, if required, the implementation will call a function in
    the course of the evaluation of that expression? As far as I know, the
    standard does not set linguistic precedence for the using the active voice
    (we call a function/constructor) as opposed to the passive voice (a
    function/constructor is called) in either case. So do you take a
    linguistice license in the case of functions but not in the case of
    constructors? If so, why?


    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 18, 2006
  9. shsingh

    Bo Persson Guest

    You can call a function.

    You cannot call a constructor directly, because it doesn't have a
    name. Most attempts to "call a constructor" will create a temporary of
    the class type instead.

    class C {};

    C::C(); // creates a temporary of class C

    In practice, you invoke the constructor as part of executing a new
    expression, like placement new.

    Bo Persson
    Bo Persson, Oct 18, 2006

  10. Come on, can't you spot a troll?

    If you create a temporary, doesn't it in fact mean you managed to call
    a constructor? Kai-Uwe is just f****ng with you. Now, you've joined
    the debacle by introducing your "directly" into the argument. So what
    does "you cannot call a constructor directly" actually mean? Does it
    mean you *can* call it, or that you *cannot* call it? Drop the
    "directly" and explain. Can you? You can't. Directly, I mean. :)
    (now that's what I call a word play)
    "Invoke", "call"... Isn't it all the same thing? At some point in
    the past I grew tired of this debate and now try not to beat this
    half-dead horse. Refraining from torturing the poor animal is highly
    recommended to all readers.

    Victor Bazarov, Oct 18, 2006
  11. shsingh

    Kai-Uwe Bux Guest

    I have read this argument several times. However, I do not understand the
    inference: I agree that constructors do not have names. But where in the
    standard do you find the other hypothesis needed for the inference, namely
    that you need a name to call someting? This additional assumption might be
    something that you consider natural because functions are called by using
    their names. However, constructor calls do not need to be parallel to
    function calls. In my view, it is perfectly natural to regard placement new
    as a special syntax provided by the standard for calling constructors.
    This syntax does not require constructors to have names, since the class
    name is used instead. The compiler then deduces for you which constructors
    is to be used (somewhat like it deduces which function you meant to call in
    the case of overloaded functions). So again: where in the standard do you
    find the provision that something needs to have a name for us to be able to
    call it?

    And how does that not call a constructor? If I am not mistaken, an
    expression of the form classname( arguments ) is even named an "explicit
    constructor call" in the standard.

    So I can invoke the constructor but I cannot call it? Where in the standard
    would I find the distinction between "to invoke" and "to call". As far as I
    can see, the two words are both used synonymously in the standard with
    regard to constructor calls.


    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 20, 2006
  12. shsingh

    440gtx Guest

    How to call a constructor explicitly if we want to allocate
    This thread has been an excellent read for me. "placement new" is a
    concept I had not heard of before. Since I do some kernel level
    development it is an excellent language feature because you often are
    given context buffer space that you overlay a structure onto. Without
    placement new, there ar two suboptimal solutions: either avoid
    constructors or put a pointer to the object (instead of the object
    itself) in the context buffer and new/delete it.

    If there are any other concepts that might especially be useful in
    kernel level development, please mention.
    440gtx, Oct 21, 2006
  13. * Kai-Uwe Bux:
    [about "can't call constructor" urban myth]

    I recently ran into yet another formulation in the standard that uses
    the "call" terminology, namely ยง1.9/15, about an initializer like

    Foo object( arg1, arg2, arg3 ... );

    "the resulting construct is a function call upon a constructor function,
    with expression-list as an argument list; such a function call is a

    Can't be very much more clear than that, I think. ;-)
    Alf P. Steinbach, Oct 21, 2006
  14. shsingh

    Bart Guest

    There are many non-portable ways that you could manage objects at a
    fixed memory location in a kernel (finding the address of the
    constructor, relocating the stack, telling the linker where to
    construct the objects) but there are at least a few ways that you could
    do it using standard C++ features:

    - placement new
    - overloaded new operator
    - pointers to fixed addresses (works for POD only)
    As mentionned above, you can overload new and delete. You can also
    provide your own allocator to the standard containers. Such things
    might be useful in a kernel implementation. However, some features such
    as exceptions and RTTI may be unavailable or may need special

    The following link may be interesting:

    Bart, Oct 21, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.