In this question creating a factory method when the compiler doesn't support new and placement new is discussed. Obviously some suitable solution could be crafted using malloc() if all necessary steps done by placement new are reproduced in some way.
What does placement new do - I'll try to list and hope not to miss anything - except the following?
What other actions are there?
Placement new is a variation new operator in C++. Normal new operator does two things : (1) Allocates memory (2) Constructs an object in allocated memory. Placement new allows us to separate above two things. In placement new, we can pass a preallocated memory and construct an object in the passed memory.
A placement new expression first calls the placement operator new function, then calls the constructor of the object upon the raw storage returned from the allocator function.
With std::vector , a memory buffer of the appropriate size is allocated without any constructor calls. Then objects are constructed in place inside this buffer using "placement new".
Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly 3 types of constructors in C++, Default, Parameterized and Copy constructors.
Placement new
does everything a regular new
would do, except allocate memory.
I think you've essentially nailed what happens, with some minor clarifications:
The order of construction/initialization is as follows:
set vtable pointer accordingly
This part is almost completely implementation-defined. Your compiler might not use vtables. There may be multiple vtable pointers, or one or more pointers to things which are not vtables. Multiple inheritance is always entertaining, as are virtual base classes. This metadata is not guaranteed to be copyable with memcpy
to another object, so the pointer(s) needn't be absolute. There could be offsets in there which are relative to the object pointer itself.
IIRC what commonly happens is that the base class constructor is called, then the vtable pointer is set to the base class, then the first derived class constructor is called, etc. This is in order to satisfy the requirements in the specification concerning what happens when a virtual function is called in a constructor. As far as I remember, there is no "list of actions" in the standard, just a defined initialisation order.
So it is not possible to generalise what an implementation does, especially since what you have is not an implementation of the C++ standard. If it cuts corners by leaving out "new", presumably with good reason because it thinks you shouldn't be using it on the target platform, then who knows what other rules of the language it ignores. If it were possible to mock up "new" with a malloc and a bit of pointer-pushing, then why on earth does the compiler not just implement new? I think you need to ask questions tagged with your specific compiler and platform, so that any experts on your compiler can respond.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With