how to clear stl::set constructed using placement new

Started by
6 comments, last by PolarWolf 6 years ago

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?

Advertisement

You act with the code like you would act with every other heap allocated code. A simple


delete pSet;

should/will call this containers destructor as any stl container has one freeing any space allocated on the heap using the defined allocator.

You are not working on the stack so this might cause your confusion because stack allocated stuff mostly causes the compiler to call the destructor on leaving the scope (anything that is inside brackets is in it's own scope) it is in without the need to manually delete it while pointer code is used to share stuff between functions/scopes

Just now, Shaarigan said:

You act with the code like you would act with every other heap allocated code. A simple



delete pSet;

should/will call this containers destructor as any stl container has one freeing any space allocated on the heap using the defined allocator.

You are not working on the stack so this might cause your confusion because stack allocated stuff mostly causes the compiler to call the destructor on leaving the scope (anything that is inside brackets is in it's own scope) it is in without the need to manually delete it while pointer code is used to share stuff between functions/scopes

I don't want to delete the pointer because I want to reuse it,in fact I used a memory pool to allocate stl::set; 

What is the reasoning behind the use of placement new here?  Is it related to your wanting to keep a pointer for future use?

If so, then you may be better off reducing `pSet` to `std::set<int>` and using std::set::clear instead.  This may (depending on the underlying implementation) give `std::set<int>` to reuse its allocated memory rather than freeing it and reallocating later as you are trying to do.  As it stands, the example can be reduced to `std::set<int>* pSet = new std::set<int>;`.

If you are attempting to localize where `std::set<int>` allocates its elements, then you must give it a different allocator, such as a linear allocator.

And, most types have a destructor.  In the example above, the destructor for `std::set<int>` can be called like so:  `pSet->~set()`.

 

If you are using a memory pool you also need to use a custom allocator to your memory pool, otherwise the internal container code will still allocate on the heap btw.

fastcall22:

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.

pSet->~set(),are you sure this code is correct? I have tried found no stl::set destructor available. 

Shaarigan: 

I only want to allocate stl::set itself using memory pool, not its elements.

ps->~set<int>() ;

this works.

This topic is closed to new replies.

Advertisement