Re: Problem with calloc
ds wrote:
Hello all
and thanks for the answers!!! I really got unstuck, I was wondering
why calloc wouldn't allocate memory the same way for an object just as
new (since I already could observe the difference). Note, that without
virtual methods, calloc works. However it is true that the object is
not constructed appropriately as is the case with new. However, if I
explicitly construct the elements using
No need to cast to void*.
where ptr is the pointer to the object and TYPE the type of the
object, the object is properly constructed. I of course understand
that this is not the correct way.
Actually, it's fine. It's called "placement new", and it exists
specifically for doing what you did, constructing in the memory you
have acquired somehow, beforehand.
Just don't 'delete' that pointer. Use the explicit destructor call
and then deallocate your memory as you normally would:
ptr->~TYPE();
free(ptr);
However, since I plan to perform the
migration iteratively, I would like for the moment to maintain the "C"
styke memory management, check that the algorithm works, and change it
alltogether to any prper C++ memory management scheme that we will
decide on.
Up to you.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask