the placement new in the second line caused memory increased,but there isn't any std::set destructor available,so how to properly clear std::set constructed using placement new,when I don't want to delete the pointer because I want to keep the pointer for future use?
Because I have hundred of sets,and these sets tend to be released and reallocted as the program running, so I used a memory pool to allocate set memory, so i have to use placement new.
Not really convinced as to why that would be required; but if it is, something along these lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// create a convenient type alias
using set_type = std::set<int> ;
// allocate memory to hold the set
char* buffer = newchar[ sizeof(set_type) ] ;
for( int i = 0 ; i < 100 ; ++i )
{
// construct a new set using placement new
set_type* ps = new (buffer) set_type ;
// use the set
// destroy the set
ps->~set_type() ;
}
// deallocate memory
delete[] buffer ;
Works like a magic,exactly what i want,though I cannot make out why these code works
using set_type = std::set<int> ; ps->~set_type() ;
it seams that create a type alias is necessary.
Notice how empty set and set with 50 values have identical size.
Similarly, empty vector and vector with 50 values have identical size.
You attempt to "optimize" those small stubs with your placements. Is that really your intention?
What is not included in those sizes? The memory allocated for the actual data (of 50 int).
Where is that then? Wherever the allocator puts them.
See http://www.cplusplus.com/reference/set/set/set/ for 'alloc'.
I don't want to delete the pointer because I want to keep the pointer for future use
<pedantic> One does not simply delete a pointer. You delete/deallocate objects/memory that have been dynamically allocated. You call delete with the address of that memory. The address is stored in a pointer, but the delete does not change the pointer (except makes it invalid).
You attempt to "optimize" those small stubs with your placements. Is that really your intention?
Yes it's my intention,because there is a lots of, and will allocate and deallocate during running.In fact the memory pool is responsible for nearly all program memory allocation.
<pedantic> One does not simply delete a pointer. You delete/deallocate objects/memory that have been dynamically allocated. You call delete with the address of that memory. The address is stored in a pointer, but the delete does not change the pointer (except makes it invalid).
My fault I didn't make it clear, I did mean to reuse the memory,not the pointer variable.