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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With