Overriding operator new/delete?

This is a discussion on Overriding operator new/delete? within the C++ Programming forums, part of the General Programming Boards category; Quick question here: Is it actually possible to override the new/delete operators with your own, custom ones? I wanted to ...

  1. #1
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768

    Overriding operator new/delete?

    Quick question here:
    Is it actually possible to override the new/delete operators with your own, custom ones? I wanted to experiment a little, but I wanted the same syntax as the new/delete.
    I don't know if it's possible, though?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can overload the new and delete operators for your class.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Ah, but not global ones, eh?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it actually possible to override the new/delete operators with your own, custom ones?
    Yes, it is. Do you want to replace the global new and delete or add custom functionality to one of your classes?
    My best code is written with the delete key.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Quote Originally Posted by Elysia View Post
    Quick question here:
    Is it actually possible to override the new/delete operators with your own, custom ones? I wanted to experiment a little, but I wanted the same syntax as the new/delete.
    I don't know if it's possible, though?
    Yes you can. Both as a class operator overload or globally. You should use the overloaded ones pretty much the same way you use the default ones. Just don't forget that in the case of class based overloading new and delete are static when declared as members.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    I wanted to override the global ones, in so that I could use my own allocation system.
    But I don't know quite how to do it.
    Just adding a new operator new at global level will, of course, gives function redefinition error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Something like...

    Code:
    #include <cstdlib> // for malloc
    #include <exception> // bad_alloc probably
    #include <new>
    
    void* operator new (size_t size) { /*... malloc(), throw, et cetera */ }
    void operator delete (void *p) { /* ... free(), et cetera ... */ }
    You do this in the global namespace.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just adding a new operator new at global level will,
    >of course, gives function redefinition error.
    Post your code. Also, be sure to match the declaration exactly:
    Code:
    #include <cstddef>
    #include <iostream>
    #include <exception>
    #include <new>
    
    void *operator new ( std::size_t size )
      throw ( std::bad_alloc )
    {
      std::cout<<"new\n";
      return 0;
    }
    
    void operator delete ( void *p )
      throw()
    {
      std::cout<<"delete\n";
    }
    
    int main()
    {
      int *x = new int;
      delete x;
    }
    My best code is written with the delete key.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Just doing this

    Code:
    void *operator new ( std::size_t size )
      throw ( std::bad_alloc )
    {
      std::cout<<"new\n";
      return 0;
    }
    
    void operator delete ( void *p )
      throw()
    {
      std::cout<<"delete\n";
    }
    Gives a bunch of errors, the most critical one being:

    Code:
    error C2365: 'operator new' : redefinition; previous definition was 'function'
    The rest are related to that one.
    Btw, Visual Studio doesn't support any types in throw, just ... or empty. But it doesn't seem to come to that.
    I'm going to try a fresh project first and see if that helps.

    EDIT: Yep, it's probably a header clash. It works fine in a fresh project. Hmmm. Is it MFC?
    EDIT2: Yup, that's the culprit. MFC is conflicting with overriding the new/delete operators somehow.
    Last edited by Elysia; 02-25-2008 at 10:35 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Are you by any chance using a library that already redefines new and delete?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    MFC does, apparently.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    yup. Just saw your edits
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Hmm. So how to work around this? It it possible to hide or is the way just to rename it to something else?
    EDIT: Made some progress now. Now I just get multiple symbols error. Not sure how to fix that yet, however.
    EDIT2: Actually, after just another compile, it compiled fine, so I successfully override(ed?) them. Now let's see see if I can override the debug new too.
    Last edited by Elysia; 02-25-2008 at 10:50 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    I did it!
    For anyone who is interested in how, it seems the new macro was getting in the way (as you know, MFC defaults to defining new as DEBUG_NEW in debug builds), so I had to push the macro, define my new, and then pop it.
    At first I got multiple symbols errors, but when compiling again, they disappeared and when running the code, the compiler called the correct function!

    Code:
    #pragma push_macro("new")
    #undef new
    
    void* __cdecl operator new (std::size_t size)
    {
    	std::cout<<"new\n";
    	return 0;
    }
    
    void* __cdecl operator new(std::size_t size, LPCSTR strLine, int nLine)
    {
    	std::cout<<"new\n";
    	return 0;
    }
    
    void operator delete (void* p)
    {
    	std::cout<<"delete\n";
    }
    
    #pragma pop_macro("new")
    The only problem is that I can't call the original operator new >_<
    But I might be able to call placement new, which I'm not very familiar with the syntax...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function overriding
    By csvraju in forum C++ Programming
    Replies: 1
    Last Post: 06-11-2009, 06:01 AM
  2. Overriding
    By vb.bajpai in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 04:28 AM
  3. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 12:26 AM
  4. overloading and overriding
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 02:29 PM
  5. redefining and overriding members
    By strobe9 in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2002, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21