0

In a file from squid, I've read the following comment:

/* Any code using libstdc++ must have externally resolvable overloads
 * for void * operator new - which means in the .o for the binary,
 * or in a shared library. static libs don't propogate the symbol
 * so, look in the translation unit containing main() in squid
 * for the extern version in squid
 */

I wonder where that comes from, where such a requirement is stated, and to which versions of gcc resp. libstdc++ it applies. The way I see it, the statement doesn't appear to be true on my system: things work fine without squid providing these symbols, and in fact providing them appears to cause crashes. On the other hand, I think the developers didn't add that comment and the overloaded operators back in 2003 just for the fun of it. I guess they were required in some configurations at that time.

Can you tell me for which configurations (versions, architectures, settings, …) adding such operator definitions might have been a requirement, and why?

MvG
  • 57,380
  • 22
  • 148
  • 276
  • 1
    possible duplicate of [Why would one replace default new and delete operators?](http://stackoverflow.com/questions/7149461/why-would-one-replace-default-new-and-delete-operators) – Alok Save Mar 30 '14 at 07:26
  • @AlokSave: Maybe I'm missing something, but the way I read it, that question discusses why I might *want* to overload them, whereas I'm asking about when I would *have to* overload them. The comment doesn't say “must have … if it wants to achieve something special”, it says “must have“ these overloads, in any case and without condition. – MvG Mar 30 '14 at 07:34
  • 1
    This seems dubious to me. The global new/delete operators *must* be replaceable, and this is typically done by implementing the defaults as [weak symbols](http://en.wikipedia.org/wiki/Weak_symbol) in the C++ runtime library; or by direct compiler intervention when it encounters a user-defined (external) symbol. A quick check of `lib/util.c` shows that `xmalloc` and `xfree` don't honour the throwing semantics of first `operator new`. These overrides are asking for trouble. – Brett Hale Mar 30 '14 at 12:07
  • As a rule, if you're going to override one, you should override the 8 replaceable new/delete operators. The throwing variants, the non-throwing variants, and the throwing/non-throwing array variants. Have a look at LLVMs libc++ [new.cpp](http://llvm.org/svn/llvm-project/libcxx/trunk/src/new.cpp) for a guide to the correct semantics for replacement new/delete operators. You can omit the `noexcept` specification if you're not using C++11. – Brett Hale Mar 30 '14 at 12:13

0 Answers0