c++11 - keeping track of allocated sizes if using std::allocator -
if use std::allocator in myvector<t, std::allocator<t> > (reimplementation of std::vector) , have expanding function
inline void expand(std::size_t e){ // expand capacity e elements t * p = m_allocator.allocate(e); ... store pointers [p, p+e] internally ... } m_allocator internal instance of std::allocator<t>.
if delete instance of myvector, delete pointers [p,p+e] through allocator (pseudo loop)
for p in [p,p+e]: m_allocator.destroy(p) m_allocator.deallocate(p,e) because std::allocator wants same arguments in deallocate function used in allocation function, need store number e somewhere internally. can avoided, doing like:
for p in [p,p+e]: m_allocator.destroy(p) m_allocator.deallocate(p,1)
Comments
Post a Comment