How should I write ISO C++ Standard conformant custom new and delete operators?

前端 未结 4 1377
旧巷少年郎
旧巷少年郎 2020-11-22 17:07

How should I write ISO C++ standard conformant custom new and delete operators?

This is in continuation of Overloading new and delete in

4条回答
  •  天涯浪人
    2020-11-22 17:40

    Implementing a custom delete operator

    The C++ Standard(§18.4.1.1) library defines operator delete as:

    void operator delete(void*) throw();
    

    Let us repeat the exercise of gathering the requirements for writing our custom operator delete:

    Requirement #1: It shall return void and its first parameter shall be void*. A custom delete operator can have more than one parameter as well but well we just need one parameter to pass the pointer pointing to the allocated memory.

    Citation from the C++ Standard:

    Section §3.7.3.2.2:

    "Each deallocation function shall return void and its first parameter shall be void*. A deallocation function can have more than one parameter....."

    Requirement #2: It should guarantee that it is safe to delete a null pointer passed as an argument.

    Citation from C++ Standard: Section §3.7.3.2.3:

    The value of the first argument supplied to one of the deallocation functions provided in the standard library may be a null pointer value; if so, the call to the deallocation function has no effect. Otherwise, the value supplied to operator delete(void*) in the standard library shall be one of the values returned by a previous invocation of either operator new(size_t) or operator new(size_t, const std::nothrow_t&) in the standard library, and the value supplied to operator delete[](void*) in the standard library shall be one of the values returned by a previous invocation of either operator new[](size_t) or operator new[](size_t, const std::nothrow_t&) in the standard library.

    Requirement #3: If the pointer being passed is not null, then the delete operator should deallocate the dynamic memory allocated and assigned to the pointer.

    Citation from C++ Standard: Section §3.7.3.2.4:

    If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, render-ing invalid all pointers referring to any part of the deallocated storage.

    Requirement #4: Also, since our class-specific operator new forwards requests of the "wrong" size to ::operator new, We MUST forward "wrongly sized" deletion requests to ::operator delete.

    So based on the requirements we summarized above here is an standard conformant pseudo code for a custom delete operator:

    class Base
    {
        public:
            //Same as before
            static void * operator new(std::size_t size) throw(std::bad_alloc);
            //delete declaration
            static void operator delete(void *rawMemory, std::size_t size) throw();
    
            void Base::operator delete(void *rawMemory, std::size_t size) throw()
            {
                if (rawMemory == 0)
                {
                    return;                            // No-Op is null pointer
                }
    
                if (size != sizeof(Base))
                {
                    // if size is "wrong,"
                    ::operator delete(rawMemory);      //Delegate to std::delete
                    return;
                }
                //If we reach here means we have correct sized pointer for deallocation
                //deallocate the memory pointed to by rawMemory;
    
                return;
            }
    };
    

提交回复
热议问题