Thread: overloading new/delete

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    overloading new/delete

    In my current game project I am worried about memory leaks, and so I want to track objects being created and deleted in the heap.

    i figured i could probably do this 2 ways.

    The first way: I could use the preprocessor and do a #define on new and delete, overwriting their meanings.

    That, however, is hackish and not good.

    The better way would be to overload the new and delete operators. I have not, however, overloaded these two specific operators before, although I have overloaded pretty much about ever other operator in C++.

    Basically ALL I want to do is have it do the EXACT same thing as the original new/delete keywords, but print out a message that an object is being created or deleted.

    Tell me if you think this code would do the trick:

    Code:
    void *operator new ( site_t t )
    {
    	GloballyDeclaredDebugOutput << "object created...\n";
    	return malloc( t );
    }
    
    void operator delete ( site_t t )
    {
    	GloballyDeclaredDebugOutput << "object destroyed...\n";
    	free( t );
    }
    My Website

    "Circular logic is good because it is."

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Here is what I found in a reference book:

    Code:
    void  operator delete(void *P)
    
    {
        cout << "Here is delete";
        free(p);
    }
    
    void *operator new(size_t size)
    {
        void *p;
        p = malloc(size);
        if (!p)
         bad_alloc ba;
         throw ba;
       
        return p;
    }
    Mr. C: Author and Instructor

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    What compiler are you using? Some have the debug versions of new and delete keep track of how many times you've called them, where they were called, wether or not the memory that was allocated is still present, yadda yadda. If they do, they usually give you functions to track everything.

    edit:
    Also, I don't believe the way you're going about it is correct. The way you have it declared, you have to send it the size of the object, which can't be done the same way you would normally call new or delete. The only time you would really want to overload either of them is if you were to set up some sort of memory partitioning, kind of like what can be found here:
    http://www.bearcave.com/software/c++_mem.html


    if you were to output a string everytime you new and delete an object, you're gonna have a hell of a time tracking things down. I would suggest keeping track of how many times each is called instead.

    Either way, you wouldn't have to redefine the operators, you could add that code to the constructors and destructors. That will also make it easier to track down leaks.
    Last edited by skorman00; 06-28-2004 at 10:48 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about instead, put a static data member in each object, and when you call your constructor and destructor, increment and decrement accordingly. You could also log each construct and destruct at the same time. Or perhaps, only on destruct, with the total number of remaining objects.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Well, there is a course at my college (CS 240) which overloads the new and delete operators to be used with all projects in that course.

    The reason they do so is so that they can track memory leaks. Basically they just count how many times new and delete are called, and make sure they are the same number at the end of the program. They also have a function to get the heap size and make sure that it is zero at the end of the program.

    They overloaded them and called them new240 and delete240 (since the class is CS240).

    I thought this was a good idea for tracking memory leaks, and so I want to implement it in my game project.

    But I want to be able to continue calling them new and delete and not have to change every instance of naming.

    Their way of doing it can be found here:
    http://faculty.cs.byu.edu/~rodham/cs...s240utils.html

    They use size_t as their parameter, and the inside of their overloaded new operator looks remarkably similar to Mister C's way of doing it.

    I have also read on websites that you are supposed to use size_t when overloading the new operator.

    I'll try Mister C's way and see if it works.

    By the way: I am using DevC++, therefore the compiler is gcc (or g++ actually)
    My Website

    "Circular logic is good because it is."

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Awesome. Looks like Mister C's way worked.

    Thanks.

    I will just modify it to count how many times new and delete are called, and then I can check for memory leaks.

    Here is the code:

    Code:
    void  operator delete(void *p)
    {
        std::cout << "Here is delete\n";
        free(p);
    }
    
    void *operator new(size_t size)
    {
        std::cout << "Here is new\n";
    
        void * p = malloc(size);
        if (p == 0) {
            throw std::bad_alloc();
        }
        return p;
    }
    Vote: put this thread on the FAQ board or put that code on the FAQ.

    [edit]

    After using my new variations of new and delete, I can tell I have a problem with memory leaks

    Calls to new: 7545
    Calls to delete: 702

    [/edit]
    Last edited by DavidP; 06-29-2004 at 09:01 AM.
    My Website

    "Circular logic is good because it is."

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Thanks for the complement.
    But I had to use a reference book to find it.

    overloading new and delete are not common and usually I have to look it up. As well as overloading the comma, and other non usual things.

    As I have said I might not know everything but I do know where I can look to find a possible solution.
    Mr. C: Author and Instructor

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question ,

    overloading new and delete are not common and usually I have to look it up. As well as overloading the comma, and other non usual things.
    The only question I have is.. why overload a comma..
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    lol i didnt know you could overload the comma
    My Website

    "Circular logic is good because it is."

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Here is how you would overload the comma:

    It is a binary operator

    Code:
    loc loc::operator, (loc op2)
    {
        loc temp;
    
        temp.longitude = op2.longitude;
        temp.latitude= op2.latitude;
        cout << op2.longitude << " " << op2.latitude << endl;
        return temp;
    
    }
    where loc is the class name & latitude and longitude are the members.
    Mr. C: Author and Instructor

  11. #11
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    haha that's crazy ive never even thought about overloading the comma before.

    ah well i'm sure it has its uses...

    this thread definitely should be moved to the FAQ board.
    My Website

    "Circular logic is good because it is."

  12. #12
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    David P,

    You can overloaded any operator in C++ except for:


    . (dot)

    :: (scope operator)

    .* (pointer to member)

    ? (with : -conditional operator)

    # (stringize)

    and


    ## (pasting)

    # and ## are technically not operators

    Hope this helps
    Mr. C: Author and Instructor

  13. #13
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    yes i know that Mister C, i'm not a 9th grade beginner

    i just never even fathomed overloading the comma
    My Website

    "Circular logic is good because it is."

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i just never even fathomed overloading the comma
    That's because nobody told you because it's worthless. The semantics for the overloaded comma are different from the built-in operator in subtle and dangerous ways. For example, the overloaded comma is not a sequence point and you can't guarantee left-to-right evaluation with a function call. Both of these are expected behavior for the comma operator, so there's no use for overloading it except when you want obfuscated code. There are similar problems with overloading && and ||. Best to stay away from overloading those too.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Replies: 6
    Last Post: 09-14-2006, 10:46 PM
  4. Overloading new/delete with additional arguments
    By _Elixia_ in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2004, 06:26 AM
  5. overloading new/delete with STL problem?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 06-23-2002, 03:17 PM