سؤال

In his new book TC++PL4, Stroustrup casts a slightly different light on a once usual practice regarding user-controlled memory allocation and placement new—or, more specifically, regarding the enigmatical "placement delete." In the book's sect. 11.2.4, Stroustrup writes:

The "placement delete" operators do nothing except possibly inform a garbage collector that the deleted pointer is no longer safely derived.

This implies that sound programming practice will follow an explicit call to a destructor by a call to placement delete.

Fair enough. However, is there no better syntax to call placement delete than the obscure

::operator delete(p);

The reason I ask is that Stroustrup's sect. 11.2.4 mentions no such odd syntax. Indeed, Stroustrup does not dwell on the matter; he mentions no syntax at all. I vaguely dislike the look of ::operator, which interjects the matter of namespace resolution into something that properly has nothing especially to do with namespaces. Does no more elegant syntax exist?

For reference, here is Stroustrup's quote in fuller context:

By default, operator new creates its object on the free store. What if we wanted the object allocated elsewhere?... We can place objects anywhere by providing an allocator function with extra arguments and then supplying such extra arguments when using new:

void* operator new(size_t, void* p) { return p; }

void buf = reinterpret_cast<void*>(0xF00F);
X* p2 = new(buf) X;

Because of this usage, the new(buf) X syntax for supplying extra arguments to operator new() is known as the placement syntax. Note that every operator new() takes a size as its first argument and that the size of the object allocated is implicitly supplied. The operator new() used by the new operator is chosen by the usual argument-matching rules; every operator new() has a size_t as its first argument.

The "placement" operator new() is the simplest such allocator. It is defined in the standard header <new>:

void* operator new (size_t, void* p) noexcept;
void* operator new[](size_t, void* p) noexcept;

void* operator delete (void* p, void*) noexcept; // if (p) make *p invalid
void* operator delete[](void* p, void*) noexcept;

The "placement delete" operators do nothing except possibly inform a garbage collector that the deleted pointer is no longer safely derived.

Stroustrup then continues to discuss the use of placement new with arenas. He does not seem to mention placement delete again.

هل كانت مفيدة؟

المحلول

If you don't want to use ::, you don't really have to. In fact, you generally shouldn't (don't want to).

You can provide replacements for ::operator new and ::operator delete (and the array variants, though you should never use them).

You can also, however, overload operator new and operator delete for a class (and yes, again, you can do the array variants, but still shouldn't ever use them).

Using something like void *x = ::operator new(some_size); forces the allocation to go directly to the global operator new instead of using a class specific one (if it exists). Generally, of course, you want to use the class specific one if it exists (and the global one if it doesn't). That's exactly what you get from using void *x = operator new(some_size); (i.e., no scope resolution operator).

As always, you need to ensure that your news and deletes match, so you should only use ::operator delete to delete the memory when/if you used ::operator new to allocate it. Most of the time you shouldn't use :: on either one.

The primary exception to that is when/if you're actually writing an operator new and operator delete for some class. These will typically call ::operator new to get a big chunk of memory, then divvy that up into object-sized pieces. To allocate that big chunk of memory, it typically (always?) has to explicitly specify ::operator new because otherwise it would end up calling itself to allocate it. Obviously, if it specifies ::operator new when it allocates the data, it also needs to specify ::operator delete to match.

نصائح أخرى

First of all: No there isn't.

But what is the type of memory? Exactly, it doesn't have one. So why not just use the following:

typedef unsigned char byte;

byte *buffer = new byte[SIZE];

Object *obj1 = new (buffer) Object;
Object *obj2 = new (buffer + sizeof(Object)) Object;
...
obj1->~Object();
obj2->~Object();

delete[] buffer;

This way you don't have to worry about placement delete at all. Just wrap the whole thing in a class called Buffer and there you go.

EDIT

I thought about your question and tried a lot of things out but I found no occasion for what you call placement delete. When you take a look into the <new> header you'll see this function is empty. I'd say it's just there for the sake of completeness. Even when using templates you're able to call the destructor manually, you know?

class Buffer
{
    private:
        size_t size, pos;
        byte *memory;

    public:
        Buffer(size_t size) : size(size), pos(0), memory(new byte[size]) {}

        ~Buffer()
        {
            delete[] memory;
        }

        template<class T>
        T* create()
        {
            if(pos + sizeof(T) > size) return NULL;
            T *obj = new (memory + pos) T;
            pos += sizeof(T);
            return obj;
        }

        template<class T>
        void destroy(T *obj)
        {
            if(obj) obj->~T(); //no need for placement delete here
        }
};


int main()
{
    Buffer buffer(1024 * 1024);

    HeavyA *aObj = buffer.create<HeavyA>();
    HeavyB *bObj = buffer.create<HeavyB>();

    if(aObj && bObj)
    {
        ...
    }

    buffer.destroy(aObj);
    buffer.destroy(bObj);
}

This class is just an arena (what Stroustrup calls it). You can use it when you have to allocate many objects and don't want the overhead of calling new everytime. IMHO this is the only use case for a placement new/delete.

This implies that sound programming practice will follow an explicit call to a destructor by a call to placement delete.

No it doesn't. IIUC Stroustrup does not mean placement delete is necessary to inform the garbage collector that memory is no longer in use, he means it doesn't do anything apart from that. All deallocation functions can tell a garbage colector memory is no longer used, but when using placement new to manage memory yourself, why would you want a garbage collector to fiddle with that memory anyway?

I vaguely dislike the look of ::operator, which interjects the matter of namespace resolution into something that properly has nothing especially to do with namespaces.

"Properly" it does have to do with namespaces, qualifying it to refer to the "global operator new" distinguishes it from any overloaded operator new for class types.

Does no more elegant syntax exist?

You probably don't ever want to call it. A placement delete operator will be called by the compiler if you use placement new and the constructor throws an exception. Since there is no memory to deallocate (because the pacement new didn't allocate any) all it does it potentially mark the memory as unused.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top