How do you declare and use an overloaded pool operator delete?
Asked Answered
E

2

8

I would like to know how to adapt section 11.14 of the C++-FAQ-lite to arrays.

Basically, I would want something like this:

class Pool {
public:
  void* allocate(size_t size) {...}
  void deallocate(void* p, size_t size) {...}
};

void* operator new[](size_t size, Pool& pool) { return pool.allocate(size); }
void operator delete[](void* p, size_t size, Pool& pool) { pool.deallocate(p, size); }

struct Foo {...};

int main() {
  Pool pool;

  Foo* manyFoos = new (pool) Foo [15];

  /* ... */

  delete [] (pool) manyFoos;
}

However, I have not been able to figure out the correct syntax to declare and call this operator delete[] (pool). Can anybody help here?

Elate answered 24/2, 2010 at 0:3 Comment(0)
E
1

It is impossible. Bjarne reasons that you'll never get it right figuring out the correct pool. His solution is: you must manually call all destructors and then figure out the correct pool to be able to deallocate the memory manually.

References:

Bjarne's FAQ: Is there a placement delete?

Relevant C++ standard sections:

3.7.3.2.2 Only member operator delete functions with an argument of size_t type are considered for delete expressions.

5.3.5.1 Delete expression syntax does not allow extra parameters.

Elate answered 24/2, 2010 at 14:25 Comment(0)
P
2

Call the dtors on the individual objects first and then use:

for (int i = 0; i < 15; ++i) manyFoos[ i ]->~Foo();
operator delete[] (manyFoos, pool);

You can read the whole FAQ item again and you will find it there.

Palmira answered 24/2, 2010 at 0:5 Comment(5)
Sorry, I don't get it. How does this statement delete the array manyFoos ? And how does the compiler know to call operator delete [], not operator delete?Elate
@Tobias: I had a typo. Fixed post.Palmira
@Tobias: When using the placement form you need to do what the compiler otherwise does for you automatically -- 1) call the dtor on the object(s) 2) free the memory. The first line of code takes care of #1. Now, the second line, takes care of freeing that part of the pool where your array objects came from.Palmira
There is no placement delete operator in C++. See:www2.research.att.com/~bs/bs_faq2.html#placement-delete for a quick reference and informit.com./articles/article.aspx?p=30642&seqNum=3Palmira
Clarification: The placement delete form does not invoke any dtors, which is why we need the explicit dtor calls.Palmira
E
1

It is impossible. Bjarne reasons that you'll never get it right figuring out the correct pool. His solution is: you must manually call all destructors and then figure out the correct pool to be able to deallocate the memory manually.

References:

Bjarne's FAQ: Is there a placement delete?

Relevant C++ standard sections:

3.7.3.2.2 Only member operator delete functions with an argument of size_t type are considered for delete expressions.

5.3.5.1 Delete expression syntax does not allow extra parameters.

Elate answered 24/2, 2010 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.