Others have already indicated that you can use the new placement. This works well if you want certain, specific class objects to be in memory allocated with malloc
. As also pointed out, when you do this, you need to explicitly call dtor.
If you want all the objects in the class to be in memory allocated with malloc
, you can overload operator new
(and operator delete
) for this class and ask them to call malloc
to get raw memory. This frees up client code for additional separate allocation / initialization steps.
If you want all objects in a collection (or more than one collection) to be in memory allocated with malloc
, you can provide a allocator for the collection to make this happen. Again, this saves the client code from working with distribution and allows the container to look, act and "feel" like a regular container.
Jerry Coffin
source share