Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a memoryleak tool. In this i am overloading new and delete operator. Its working fine. But the code for which i am creating this tool is around 15000 lines. I cannot change the existing code only i can invoke the memoryleak tool function into the existing code. Existing code having stl containers (like list, map, stack etc). Stl container also call new and delete operator to allocate or deallocate memory. i want that stl container should call the new and delete operator which are in not the overloaded new and delete. for ex:
int *iptr = new int[10] ----> should call overloaded new[] 
delete [] iptr -------------> should call overloaded delete[] 
map.insert(10) ------------->; should call default new[] ( which are in new.h) 
map.erase()  ---------------> should call default delete[] //(which are in new.h)

How can i do it? Any help would be appreciated.
Posted
Updated 19-Feb-12 19:11pm
v3
Comments
Varun Sareen 20-Feb-12 0:32am    
edit for: updated pre tag

If you overload ::operator new() and ::operator delete() in global scope, you cannot use default new and delete anymore.
You can try to put your overload operator new and delete into a Class and maybe the job you gotta do is immense.

C++
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
using namespace std;

void* operator new(size_t s)
{
    void* mem = malloc(s);
    puts("My new");

    // avoid using iostream, for it will use new & delete,that will lead to deadlock
    if (!mem) puts("out");

    return mem;
}

void operator delete(void* mem)
{
    puts("My free");
    free(mem);
}

int main()
{
    // they all invoke overload new and delete
    int* test1 = new int(10);
    int* test2 = new int[5];
    vector<int> intvec;
    vector<int>::iterator it;
    intvec.insert(it, 10);

    delete test1;
    delete []test2;
    return 0;
}
 
Share this answer
 
v2
Take a look at the template parameters for the map class -
template <
    class Key, 
    class Type, 
    class Traits = less<Key>, 
    class Allocator=allocator<pair <const Key, Type> >
>
class map

Here you can pass in a custom allocator which does the memory allocation for the map class.
In this allocator you could use malloc or HeapAlloc so that the overloaded new/delete is not called.

Here are some links on stl allocators -
http://en.wikipedia.org/wiki/Allocator_(C%2B%2B)[^]
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079[^]
 
Share this answer
 
Comments
Yerasel 20-Feb-12 4:21am    
I tried to define a simple Allocator following above statements. In my Allocator ::operator new() is inoked, and than I define vector. However it turns out that vector will call overloaded new and delete if these operators are overloaded globally.
The code as following:

#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include <limits>
using namespace std;

void* operator new(size_t s)
{
void* mem = malloc(s);
puts("My new");

// avoid using iostream, for it will use new & delete,that will lead to deadlock
if (!mem) puts("out");

return mem;
}

void operator delete(void* mem)
{
puts("My free");
free(mem);
}

template<typename t="">
class Allocator {
public :
// typedefs
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

public :
// convert an allocator<t> to allocator
template<typename u="">
struct rebind {
typedef Allocator other;
};

public :
inline explicit Allocator() {}
inline ~Allocator() {}
inline explicit Allocator(Allocator const&) {}
template<typename u="">
inline explicit Allocator(Allocator const&) {}

// address
inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) { return &r; }

// memory allocation
inline pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0) {
return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T)));
}
inline void deallocate(pointer p, size_type) {
::operator delete(p);
}

// size
inline size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}

// construction/destruction
inline void construct(pointer p, const T& t) { new(p) T(t); }
inline void destroy(pointer p) { p->~T(); }

inline bool operator==(Allocator const&) { return true; }
inline bool operator!=(Allocator const& a) { return !operator==(a); }
}; // end of class Allocator

int main()
{
// they all invoke overload new and delete
int* test1 = new int(10);
int* test2 = new int[5];
vector<int, allocator<int=""> > intvec;
vector<int, allocator<int=""> >::iterator it;
intvec.insert(it, 10);

delete test1;
delete []test2;
return 0;
}
«_Superman_» 20-Feb-12 8:57am    
You can use a custom allocator for the vector also.
I tried to define a simple Allocator following above statements. In my Allocator ::operator new() is invoked, and than I define vector. However it turns out that vector will call overloaded new and delete if these operators are overloaded globally.
The code as following:

C++
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include <limits>
using namespace std;

void* operator new(size_t s)
{
    void* mem = malloc(s);
    puts("My new");

    // avoid using iostream, for it will use new & delete,that will lead to deadlock
    if (!mem) puts("out");

    return mem;
}

void operator delete(void* mem)
{
    puts("My free");
    free(mem);
}

template<typename t="">
class Allocator {
public :
    //    typedefs
    typedef T value_type;
    typedef value_type* pointer;
    typedef const value_type* const_pointer;
    typedef value_type& reference;
    typedef const value_type& const_reference;
    typedef std::size_t size_type;
    typedef std::ptrdiff_t difference_type;

public :
    //    convert an allocator<t> to allocator<u>
    template<typename u="">
    struct rebind {
        typedef Allocator<u> other;
    };

public :
    inline explicit Allocator() {}
    inline ~Allocator() {}
    inline explicit Allocator(Allocator const&) {}
    template<typename u="">
    inline explicit Allocator(Allocator<u> const&) {}

    //    address
    inline pointer address(reference r) { return &r; }
    inline const_pointer address(const_reference r) { return &r; }

    //    memory allocation
    inline pointer allocate(size_type cnt,
       typename std::allocator<void>::const_pointer = 0) {
      return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T)));
    }
    inline void deallocate(pointer p, size_type) {
        ::operator delete(p);
    }

    //    size
    inline size_type max_size() const {
        return std::numeric_limits<size_type>::max() / sizeof(T);
 }

    //    construction/destruction
    inline void construct(pointer p, const T& t) { new(p) T(t); }
    inline void destroy(pointer p) { p->~T(); }

    inline bool operator==(Allocator const&) { return true; }
    inline bool operator!=(Allocator const& a) { return !operator==(a); }
};    //    end of class Allocator

int main()
{
    // they all invoke overload new and delete
    int* test1 = new int(10);
    int* test2 = new int[5];
    vector<int,> > intvec;
    vector<int,> >::iterator it;
    intvec.insert(it, 10);

    delete test1;
    delete []test2;
    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900