如何知道对象创建是否在堆上......?

发布于 2024-10-08 12:18:19 字数 1460 浏览 4 评论 0原文

我们希望在低延迟系统上工作,应用程序中的堆分配成本更高。但在某种程度上,允许在堆上创建对象。这就是为什么我们想要指示创建的对象是否在堆上......?

方法是找出在堆内存上创建的对象的正确方法吗

#include <iostream>
#include <set>

using namespace std;

class MemStat              //base class
{
    typedef set<MemStat*> POINTERS; 
    static POINTERS m_ptrlist;
public:
    void* operator new (size_t size)
    {
        MemStat* ptr = ::new MemStat;
        m_ptrlist.insert(ptr);
        return ptr;
    }
    void operator delete(void* dptr)
    {
        MemStat* ptr = static_cast<MemStat*>(dptr);
        m_ptrlist.erase(ptr);
        ::delete ptr;
    }
    // void* operator new[] (size_t sz);
    // void operator delete[] (void*);

    bool is_on_heap() { m_ptrlist.find(this) != m_ptrlist.end(); }

protected:             // ctor & dtor are protected for restrictions
    MemStat() { }
    virtual ~MemStat() { }
    MemStat(const MemStat&) { } 
    const MemStat& operator=(const MemStat& ) { return *this; }
};
MemStat::POINTERS MemStat::m_ptrlist;

下面的 我们需要检查堆创建的类将从 MemStat 类派生,使用 new &实例化基类对象时删除运算符调用。

class MyClass : public MemStat   //end user class
{
};

int main()
{
    MyClass* myptr = new MyClass;
    MyClass obj;

    cout << myptr->is_on_heap() << endl;    //results into yes
    cout << obj.is_on_heap() << endl;       //reults into no

    delete myptr;
}

We want to work on low latency system, heap allocation is costlier in the application. but for some extent object creation on heap is allowed. Thats why we want indication whether object is created is on heap or not..?

Is the below methodology is the correct way to find out object created on heap memory..?

Will have generic class where new and delete operator is overloaded to maintain heap allocated pointers....

#include <iostream>
#include <set>

using namespace std;

class MemStat              //base class
{
    typedef set<MemStat*> POINTERS; 
    static POINTERS m_ptrlist;
public:
    void* operator new (size_t size)
    {
        MemStat* ptr = ::new MemStat;
        m_ptrlist.insert(ptr);
        return ptr;
    }
    void operator delete(void* dptr)
    {
        MemStat* ptr = static_cast<MemStat*>(dptr);
        m_ptrlist.erase(ptr);
        ::delete ptr;
    }
    // void* operator new[] (size_t sz);
    // void operator delete[] (void*);

    bool is_on_heap() { m_ptrlist.find(this) != m_ptrlist.end(); }

protected:             // ctor & dtor are protected for restrictions
    MemStat() { }
    virtual ~MemStat() { }
    MemStat(const MemStat&) { } 
    const MemStat& operator=(const MemStat& ) { return *this; }
};
MemStat::POINTERS MemStat::m_ptrlist;

for the end user classes which we need to check for the heap creation will be derived from MemStat class uses new & delete operator call while instantiating base class object.

class MyClass : public MemStat   //end user class
{
};

int main()
{
    MyClass* myptr = new MyClass;
    MyClass obj;

    cout << myptr->is_on_heap() << endl;    //results into yes
    cout << obj.is_on_heap() << endl;       //reults into no

    delete myptr;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2024-10-15 12:18:19

请注意,只要 MyClass 对象是另一个可能或可能不是动态分配的对象的子对象(继承或包含),您的方案就会惨败。 (我所知道的防止动态分配的技巧也在此方面失败了。)

因此,您所做的只是进一步减慢堆分配速度,而没有获得太多好处。除了极少数情况外,对象的分配由类的用户决定。
如果他们认为需要动态分配一个,你有什么理由不同意呢?

Note that your scheme fails miserably as soon as a MyClass object is a sub-object (inherited or contained) of another object which might or might not by allocated dynamically. (And the tricks I know for preventing dynamic allocation fail on that one as well.)

So what you're doing just further slows down heap allocation without gaining much. Except for a few very rare circumstances, where an object is allocated is something your class' users decide.
If they think they need to dynamically allocate one, who are you to disagree?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文