From this link, it says that there are four steps for memory reallocation when using c++ vectors...
I am particularly interested in numbers 3 and 4, is it possible to perform these tasks through code? Or does it just happen in the background.
I.e., am I able to "Destroy elements in memory" through code in C++? And am I able to directly "Deallocate memory" in C++ code?
Yes, and yes! You might want to look into either the std::allocator type, or into placement new, operator new, destructor calls, and operator delete. (Note that operator new and operator delete are names of allocation and deallocation functions and not the same as the new and delete operators).
Hope this helps!
am I able to "Destroy elements in memory" through code in C++?
Yes. By calling the object's destructor. For example, if x is a reference to an object of type T, you can destroy that object with:
x.~T();
And am I able to directly "Deallocate memory" in C++ code?
Of course. There are a variety of deallocation functions, that go with corresponding allocation functions. If you allocated with malloc, you deallocate with free. If you allocated with operator new(), you deallocate with operator delete(). If you allocated with new char[], you deallocate with delete[].
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