67

(There were at least 4-5 topics with a similar topic on SO. I read each of them and I don't feel they really help me with this specific issue).

I'm using Visual Studio .NET 2003 on Windows 7.

I have my own overloads of new/delete that point to my own custom calls to malloc() and free() for diagnostics. My new/delete overloads are in a header file which I've included in a few files.

The problem is, the code base is pretty much spaghetti and there is no easy way to make sure these overloads get used by everything. There are includes to third party libraries that are black-box. We also use STL everywhere.

In my tests I've found that STL is still mixing calls to my own new/delete and the standard MSVC new/delete calls.

It doesn't seem realistic to include my header file in thousands of other files, that would just take far too long. Can anyone offer some tips on how to properly and effectively overload new/delete globally so everything uses my custom memory manager?

1
  • 1
    If you define the operators globally in a pre-compiled header that should cover the most ground. Alternately you can use the CRT heap functions if this is for detecting memory leaks.
    – AJG85
    Nov 18, 2011 at 17:09

2 Answers 2

94

That's not how this works. You replace the two operators, and this is done at link time. All you need to do is write a single TU that defines these operators and link it into the mix. Nobody else ever needs to know about this:

// optional_ops.cpp

void * operator new(std::size_t n) throw(std::bad_alloc)
{
  //...
}
void operator delete(void * p) throw()
{
  //...
}

In principle, there's no need for any header files to declare these functions (operator new, operator delete), since the declarations of those two functions are already hardcoded into the language, if you will. However, the names std, std::bad_alloc and std::size_t are not predeclared, so you will probably want to include <new> or some other header to provide those names.

In C++11 and beyond, you can alternatively use decltype(sizeof(0)) to get the size of the first parameter in a way that doesn't require any kind of library. C++11 also has a simpler exception model without dynamic exception specifications (which were finally removed from the language entirely in C++17).

void * operator new(decltype(sizeof(0)) n) noexcept(false)
{
  //...
}
23
  • 1
    Won't the linker complain about duplicate definitions? I think the ODR applies here. Not to mention we have 120 DLLs that we build, and I'd have to link it in each one of those DLL projects. I guess this is still better than the alternatives. Nov 18, 2011 at 17:12
  • 3
    @RobertDailey: Nope, special case, covered by the standard, weak references, etc etc. I actually reported a bug in GCC concerning this the other day, so with the latest version this should even work with -fwhole-program and -flto and whatnot (see here and here.)`
    – Kerrek SB
    Nov 18, 2011 at 17:13
  • 7
    @RobertDailey: The standard covers that user definitions replace the functions, so there's no violation of ODR. The compiler implements this by making the standard functions "weak" references that are overridden by the linker if another symbol of the same name is found.
    – Kerrek SB
    Nov 18, 2011 at 17:26
  • 5
    Yeah, on Windows you can't offer up replacement new/delete functions for the whole program without linking them into every DLL. Or at least forcing the dlls to import those functions, but I'm not sure if that works. I think it's unfortunate that DLLs work this way. On other platforms when shared libraries are linked at program start-up time they generally obey the same rules as statically linked translation units, so if your program replaces new/delete the shared libraries will get those replacements too.
    – bames53
    Nov 18, 2011 at 19:08
  • 5
    You might also want to override the "no throw" versions of new, which have a second parameter const std::nothrow_t& (part of the C++ standard)
    – rustyx
    Oct 2, 2015 at 12:34
44

Also add these lines:

void *operator new[](std::size_t s) throw(std::bad_alloc)
{
    // TODO: implement
    return NULL;
}
void operator delete[](void *p) throw()
{
    // TODO: implement
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.