So I came across placement new recently and had a question regarding it. I do understand that placement new is used when you want to allocate an object on an already allocated memory.
Lets say I have a class called foo, which is 20bytes.
I'd do some operations with it, and then I'll call a placement new for a class called bar, which is 10 bytes.
What happens to the last 10 bytes that is not part of the new object. Does placement new partition only the amount of memory required, which is in this case 10bytes? Or does the pointer just carry around the extra 10 bytes until it is deallocated.
And another case, lets say I've first started of by allocating memory for class bar, which is 10 bytes. I'll then placement new class foo, which is 20 bytes. Will the compiler allocate more memory? Or do I have to make sure to allocate enough memory beforehand?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire