how to clear stl::set constructed using placement new

I noticed when constructing std::set using placement new,like this
1
2
  std::set<int> *pSet = (std::set<int>*)new char[sizeof(std::set<int>)];
  new(pSet) std::set<int>;

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?
Last edited on
Why are you constructing std::set using placement new?
Why don't you just use value semantics?

For instance:
1
2
3
4
std::set<int> my_set ;

// and if, for some strange reason, you must have a pointer to it
std::set<int> *pSet = std::addressof(my_set) ;

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.
Last edited on
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 = new char[ 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.
This would also work: ps->~set<int>() ;
ps->~set<int>() ; bingo,I chose this one, thanks a lot.
What do you mean by "set memory"?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <set>
#include <vector>

int main ()
{
  std::set<int> myset;
  std::cout << sizeof(myset) << '\n';
  for ( int i=1; i<=50; ++i ) myset.insert(i*10);
  std::cout << sizeof(myset) << '\n';

  std::vector<int> myvec;
  std::cout << sizeof(myvec) << '\n';
  myvec.resize( 50 );
  std::cout << sizeof(myvec) << '\n';

  return 0;
}

48
48
24
24

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.
Topic archived. No new replies allowed.