简体   繁体   English

内存泄漏C ++指针

[英]Memory leak c++ pointers

int main()
{
    A* x = new B(10, 20);
    x->opt1();
    delete x;
    return 0;
}

int main()
{
    B x(10, 20);
    return 0;
}

So, the first has a memory leak, I understand that because x is an A object that points to a B object. 因此,第一个有内存泄漏,我知道因为x是指向B对象的A对象。 If A was not a pointer would it still have a memory leak? 如果A不是指针,它是否还会发生内存泄漏?

And why does the second function not have a memory leak? 为什么第二个功能没有内存泄漏? We don't need to delete x? 我们不需要删除x吗?

class A
{
    private:
        int* a;
    public:
        A(int size) {
            a = new int[size];
        }
        ~A() {
            if (a) {
                delete [] a;
            }
        };
        virtual void opt1() {
            cout << "A::opt1()" << endl;
        }
};

class B : public A
{
    private:
        float* b;
    public:
        B(int size1, int size2) : A(size1) {
            b = new float[size2];
        }
        ~B() {
            if(b){
                delete [] b;
            }
        }
        virtual void opt1() {
            cout << "B::opt1()" << endl;
        }
};

the first has a memory leak, I understand that because x is an A object that points to a B object. 第一个内存泄漏,我知道因为x是指向B对象的A对象。

No, there's no memory leak if A and B have well-defined inheritance relationship. 不,如果AB具有明确定义的继承关系,则不会发生内存泄漏。 Such as: 如:

class A {
public:
    virtual ~A() {}
};

class B : public A {
};

why does the second function not have a memory leak? 为什么第二个功能没有内存泄漏?

Because x is allocated on stack, which don't need and can't call delete on it. 因为x是在堆栈上分配的,所以不需要,也不能在其上调用delete x will be destroyed automatically. x将被自动销毁。

We don't need to delete x? 我们不需要删除x吗?

Only object created by new need to delete . 仅由new创建的对象需要delete

EDIT 编辑

For you code of A and B , the first case will lead to memory leak, because A (the base class) 's destructor is not virtual . 对于您的AB代码,第一种情况将导致内存泄漏,因为A (基类)的析构函数不是virtual For 对于

A* x = new B(10, 20);
delete x;

only the destructor of A will be called, the destructor of B won't be called, that means float* b inside B won't be deallocated. 仅会调用A的析构函数,而不会调用B的析构函数,这意味着B float* b不会被释放。

It's a bad idea if you want to use them in this way, ie deleting an instance of a derived class through a pointer to base class. 如果要以这种方式使用它们(例如,通过指向基类的指针删除派生类的实例),这是一个坏主意。

See When to use virtual destructors? 请参阅何时使用虚拟析构函数?

In the second, new was not explicitly called, though the item's constructor was. 在第二个实例中,虽然该项目的构造函数被调用,但new未被显式调用。 But the variable goes out of scope automatically as so the destructor being automatically called prevents any sort of leak, provided the destructor is correctly written to counteract any internal allocations within the object. 但是变量会自动超出范围,因此只要正确编写了析构函数以抵消对象内的任何内部分配,就可以自动调用析构函数以防止发生任何形式的泄漏。

I would think the first example would not be a memory leak, but maybe there is some fine point of a based class which causes a problem. 我认为第一个示例不会造成内存泄漏,但是也许基于类的某些优点会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM