c++ - Are there any problems with this overload of operator new? -


I was thinking about some memory pool / allocation material I can write, so I am with this operator I want to use memory to reuse the memory. I am thinking that if there is a problem then you can think with my implementation (or any other potential people).

  #include & lt; Cstddef> Namespace ns {struct renew_} {}; Renewal renewed; } Template & lt; Typename T & gt; Inline Zero * Operator new (std :: size_t size, T * p, ns :: renew_t renewal) {p- & gt; ~ T (); Return p; } Template & lt; Typename T & gt; Remove Inline Zero operator (Zero *, T *, NS :: Upgrade Renew) {}  

It can be used like this

  int Main () {Foo * p (new foo ()); // allocates memory and calls the default constructor of foo (P, NS :: Upgrade) to Foo (42); // calls foo destroyer, then calls the FU consultants on the same memory delivery p; // deallocates the destructor and memory of the call foo}  

should be good You do not try some crazy and try to renew a subclass as you said that it is for a pool, it should be fine.

He said, my only question is - is it more clear? It's a taste question, but imagine someone else may need to look at the code. You are basically contracting only two simple and clear statements that require deep insights into the code internally.

In my pool works, I usually had two different ways, one to destroy one and one, they are essentially what you are doing here (p-> ~ t and new (P) T ()), but at least you know what they really do.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -