Out of curiosity, I've been looking into how std::vector works in detail. Something I've discovered has me completely baffled.
When a vector is reallocated as a result of growing in size beyond its capacity, it creates a new array, moves (Thanks to C++11 capabilities.) the data over, then deletes the old array. When the old array is deleted, all of the destructors are called for the classes in the array.
Considering the classes were just moved (not copied) to a new memory location, what purpose can calling the destructors serve? Even if these classes had dynamically allocated memory, the addresses of that memory are still retained, no copy of that memory has been made, so no leak can occur. I absolutely cannot think of any reason that the destructors need to be called in this scenario, and I'm hoping there's just something I'm ignorant of.
If you know why this is done, or have any theories, please share.
AnthonyBMember Since 16 May 2009
Offline Last Active Jun 19 2012 04:41 PM
- Group Members
- Active Posts 9
- Profile Views 226
- Member Title Member
- Age Age Unknown
- Birthday Birthday Unknown
AnthonyB hasn't added any contacts yet.