Is it possible to completely disable the default C++ new operator?

前端 未结 4 1238
走了就别回头了
走了就别回头了 2021-02-01 14:03

Because our app has hard performance and memory constraints, our coding standards forbid the use of the default heap — ie, no malloc, no default new

相关标签:
4条回答
  • 2021-02-01 14:20

    You can implement the default new to call an unimplemented function. Then, at link time, you will get an error to the users of the bare new call:

    #include <stdexcept>
    inline void * operator new (std::size_t) throw(std::bad_alloc) {
        extern void *bare_new_erroneously_called();
        return bare_new_erroneously_called();
    }
    

    When I tested it on IDEONE, I got this error:

    /home/geXgjE/ccrEKfzG.o: In function `main':
    prog.cpp:(.text.startup+0xa): undefined reference to `bare_new_erroneously_called()'
    collect2: error: ld returned 1 exit status
    

    In my tests, using g++, there is no link error if there are no references to the bare new in the program. This is because g++ does not emit code for unused inline functions.

    I don't have Visual Studio installed on my system, so the following information is just based on some documentation I have found. In order to get the inlined new operator to be seen everywhere, you should put its definition in a header file, and then use the /FI detect_bare_new.h option in your compiler.* According to this answer, Visual Studio will not generate code for unused inline functions (like g++). However, you should check to see if there is an optimization level that needs to be enabled for that behavior or not.

    * g++ has a similar compiler option: -include detect_bare_new.h.

    This assumes that you intend to pass your own allocators to C++ templates and classes in the standard C++ library. If you do not, then inlined code in the standard headers that call the default allocator (which will call new) will trigger the linking error as well. If you wish to allow the standard C++ library to use the default new, then an easy way to make it work (at the expense of longer compile times) is to add all the standard C++ headers you intend to include at the top of the detect_bare_new.h file.

    You state that portability of the solution is not important to you. But for the sake of completeness, I should highlight the issue that Ben Voigt correctly points out: The C++ standard does not guarantee the behavior of not generating code for unused inline functions. So, one may get a linking error even if the function is not used. But, if the code has no other references to the unimplemented function except within the stubbed new implementation, the error would be within the new definition itself. For example, g++ could generate an error like:

    /home/QixX3R/cczri4AW.o: In function `operator new(unsigned int)':
    prog.cpp:(.text+0x1): undefined reference to `bare_new_erroneously_called()'
    collect2: error: ld returned 1 exit status
    

    If your system is one that generates code for unused inline functions, you may still have a workaround. The workaround will work if the linker will report all erroneous references to the undefined function. In that case, if the only linking error observed is due to the definition of the new operator itself, there are no unexpected calls to the bare new. After verifying that the code only has that single error, you could then change the link line to include an object or library that has an appropriate definition of bare_new_erroneously_called() that would throw a runtime exception.

    0 讨论(0)
  • 2021-02-01 14:21

    If your own "new" operator is not named "new" but differently (e.g. "myNew") you could use a "#define" in a way replacing "new" by rubbish:

    #define new *+-/&
    

    The pre-compiler would now replace a "new":

    x = new mytype;
    

    By the rubbish:

    x = *+-/& mytype;
    

    The advantage compared to a message at linking time is that this will generating a compiler message immediately while compiling the C++ file, not in the end when linking. You also see the line where the "new" is located.

    The disadvantage is that you'll have to "#include" the file containing this "#define" in all C++ files in your project.

    0 讨论(0)
  • 2021-02-01 14:31

    Poison it!
    If you are using GCC, there is a pragma for this:

    #ifdef __GNUC__
    
    /* poision memory functions */
    #   pragma GCC poison malloc new
    
    #endif
    
    0 讨论(0)
  • 2021-02-01 14:43

    You can just declare operator new = delete, similar to how you'd disable some constructors. Like

    struct Foo {
        void* operator new(std::size_t) = delete;
        void* operator new[](std::size_t) = delete;
    };
    

    That'll give you compiler errors when you try to use new with this class. See https://godbolt.org/z/RToOcf

    0 讨论(0)
提交回复
热议问题