重载后如何使用全局new/delete ... [英] How to use global new/delete after overloading it...

查看:89
本文介绍了重载后如何使用全局new/delete ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个内存泄漏工具.在这种情况下,我正在重载new和delete运算符.它的工作正常.但是我为其创建此工具的代码大约为15000行.我无法更改现有代码,只能将memoryleak工具功能调用到现有代码中.现有的代码具有stl容器(如列表,映射,堆栈等). Stl容器还调用new和delete运算符来分配或取消分配内存.我希望该stl容器应调用new和delete运算符,而不是在重载的new和delete中.例如:

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.

推荐答案

如果在全局范围内重载:: operator new()和:: operator delete(),则不能使用默认的new和delete.不再.
您可以尝试将新的重载运算符放到一个Class中,然后删除它,这也许是您要做的工作.

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.

#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;
}


看看地图类的模板参数-
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


在这里,您可以传入一个自定义分配器,该分配器将为地图类分配内存.
在此分配器中,可以使用mallocHeapAlloc,以便不调用重载的new/delete.

这是stl分配器上的一些链接-
http://en.wikipedia.org/wiki/Allocator_(C%2B%2B) [^ ]
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article. php/c4079 [ ^ ]


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[^]


我尝试根据上述声明定义一个简单的分配器.在我的分配器:: operator中,调用new(),然后定义矢量.但是事实证明,如果这些运算符在全局范围内被重载,向量将调用重载new并删除.
代码如下:

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:

#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;
}


这篇关于重载后如何使用全局new/delete ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆