3

I've created the following demo program regarding operator new and operator delete.

void* operator new(size_t size)
{
    void* ptr = std::malloc(size);
    std::cout << "allocated [" << size << "] bytes address [" << ptr << "]" << std::endl;
    return ptr;
}

auto operator delete(void* addr) noexcept -> void
{
    std::cout << "freed address [" << addr << "]" << std::endl;
    return std::free(addr);
}

In main, I try to do this:

int main()
{
            auto ptr = new int(10);
            delete ptr;
}

However, I could see the log of operator new but could not see that of operator delete

allocated [4] bytes address [0x192bcc41900]

It is interesting that for a class customized operator new and operator delete, there is no such problem.

class A
{
public:
    A()
    {
        std::cout << "A::ctor" << std::endl;
    }
    ~A()
    {
        std::cout << "A::dtor" << std::endl;
    }
    void* operator new(size_t size)
    {
        void* ptr = std::malloc(size);
        std::cout << "A::new allocated [" << size << "] bytes address [" << ptr << "]" << std::endl;
        return ptr;
    }

    void operator delete(void* addr) noexcept
    {
        std::cout << "A::delete freed address [" << addr << "]" << std::endl;
        return std::free(addr);
    }

private:
    int m_int = 10;
};

I can see both logs when I new a A class object and then delete it.

auto ptr = new int(10);
delete ptr;

What's wrong with this?

silentspring
  • 87
  • 1
  • 5
  • 2
    See [overloading new/delete](https://stackoverflow.com/a/583040/12002570), `void operator delete(void*, size_t);` – Jason Jul 27 '22 at 04:11
  • 1
    So far my rule of thumb was, to use `operator new` and `operator delete` only if I need access to raw memory. Overloading them for some special memory management (e.g. shared memory) is possibly also a legit use case. But there are a ton of overloads and you might have gotten the wrong one. https://en.cppreference.com/w/cpp/memory/new/operator_delete – BitTickler Jul 27 '22 at 04:14
  • Your code works as-is on the version of Clang in Mac 12.x (Monterey) (and other compilers) - what are your compilation flags and compiler version? – wkl Jul 27 '22 at 04:16
  • Your code gave the expected output https://godbolt.org/z/eTrjMvY14 – Ch3steR Jul 27 '22 at 04:17
  • 2
    Can't reproduce either. Out of curiosity, why are you using different return styles i.e. preceding return type for `operator new()` and trailing return type for the `operator delete()`? Does the behaviour you see still occur if you change `operator delete()` to use the preceding return type (i.e. `void operator delete(void *address) noexcept`)? – Peter Jul 27 '22 at 04:39
  • I am using CLion 2022.1 on Windows, and this is my CMakefile cmake_minimum_required(VERSION 3.22) project(test_new_delete) set(CMAKE_CXX_STANDARD 17) add_executable(test_new_delete main.cpp) – silentspring Jul 27 '22 at 05:37
  • 1
    Perhaps this is a clion issue. Are you sure you see all the output of the program? Try adding some prints at the end of `main`. – n. m. could be an AI Jul 27 '22 at 06:26
  • Yeah, perhaps it is CLion issue. I am pretty sure I have seen all the logs. The same program did yield expected results (both operator new and operator delete logs are printed) on Linux. – silentspring Jul 28 '22 at 00:30

0 Answers0