Thread: overloading new/delete with STL problem?

  1. #1
    Unregistered
    Guest

    Unhappy overloading new/delete with STL problem?

    I have overloaded the global new and delete operators for purpose of demonstrating my problem:

    #include <malloc.h>
    #include <stdio.h>

    int newctr = 0;
    int delctr = 0;

    inline void* operator new (size_t size)
    {
    newctr++;
    return malloc(size);
    }

    inline void* operator new [] (size_t size)
    {
    newctr++;
    return malloc(size);
    }

    inline void operator delete (void* p)
    {
    delctr++;
    free(p);
    }

    inline void operator delete [] (void* p)
    {
    delctr++;
    free(p);
    }

    void report(void)
    {
    printf("new calls: %d\n",newctr);
    printf("delete calls: %d\n",delctr);
    }

    #include <vector>

    int main(void)
    {
    atexit(report);
    std::vector v;
    return 0;
    }

    result:

    new calls: 0
    delete calls: 1

    Whenever I use STL classes they call the delete operator but not new. Does anyone know 1) Why this happens 2) How to fix it.
    Thanks,

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Yes, when you create the vector, there is no size so new is never called. But when the object is destroyed, delete is called to free any memory
    chaning the code to
    vector<int> v(2);
    gave me the results
    new calls 1
    delete calls 1

    when i added
    v.push_back(313);
    both new and delete were called 2 times.

    It seems to me that the destructors always call delete, regarless if it ever called new.

    To make sure new and delete get called the same number of times, give all stl classes a size when calling the constructor.

  3. #3
    Unregistered
    Guest

    Unhappy

    Thanks for the vector fix, however now try this:

    #include <fstream.h>

    int main(void)
    {
    atexit(report);
    fstream file;
    return 0;
    }

    Same problem. This isn't good because I've created a custom memory manager which reports memory leaks by tracking all new/delete malloc/free calls but these objects don't seem to be calling new resulting in my allocation counter being decremented and reporting bogus memory leaks. I'm not sure how to fix it.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What compiler are you using? There may be a debug version of operator new being called. Also, you should use the standard headers.

  5. #5
    Unregistered
    Guest

    Unhappy

    I'm using Visual C++ 6.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with static STL i.e. map
    By vijay_choudhari in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2007, 05:56 AM
  2. STL problem
    By ltanusaputra in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 10:56 AM
  3. problem with pointer to object and STL list
    By modec in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2005, 05:08 AM
  4. Weird problem with overloading
    By Strait in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2005, 07:34 AM
  5. STL vector <T> problem
    By correlcj in forum C++ Programming
    Replies: 11
    Last Post: 11-06-2002, 07:18 PM