Is delete necessary in a destructor?

2020-04-03 07:00发布

I have the following code and I'm wondering if that delete b is necessary here ? Will my operating system automatically clear that area of memory allocated ?

class A
{
    B *b;

    A()
    {
        b = new B();
    }

    ~A() 
    {
        delete b;
    }
};

Many thanks.

9条回答
爷的心禁止访问
2楼-- · 2020-04-03 07:32

Probably your operating system will free the allocated memory - but this is done when your program exits. Long running programs will run into memory issues.

It's always a good idea to use smart pointers for dynamically allcoated objects. These will do all the delete stuff for you.

  • std::auto_ptr
  • boost::shared_ptr
  • boost::scoped_ptr
查看更多
Melony?
3楼-- · 2020-04-03 07:36

If you call a new, corresponding delete is always advisable.

As far as operating system deleting your memory.. yes, it will happen but only after the entire process terminates (i.e. your application exits). Only then all memory and other resources are reclaimed by the OS.

Thirdly, try to use new/delete only when necessary. In your scenario, you could just write

class A 
{

B b;

  A() {}

  ~A() {}

};

It would have the same effect and you avoid an extra dynamic memory allocation.

查看更多
smile是对你的礼貌
4楼-- · 2020-04-03 07:37

The b member will be allocated on heap. It is true that the operating system will free all the memory occupied by heap; however that will happen only once: on program exit.

So if you don't free the allocated heap blocks the very time they become unused (typically in destructor of some object), you are risking to get a memory leak.

So the answer is basically: yes, you have to call delete manually, as the memory won't be freed ASAP automagically (though smart-pointers will help you to achieve something similar).

查看更多
Deceive 欺骗
5楼-- · 2020-04-03 07:38

Anything that you allocate with new you should free with delete, otherwise there is a memory leak in your application.

On most modern operating systems (certainly the OSes that people usually run on desktop computers) anything that a process uses will be cleaned up when the process ends, so if you forget a delete, then the memory will be freed anyway. However you should not rely on this.

Long ago I programmed on the Amiga in C. Its operating system was much less sophisticated than today's operating systems. If you'd allocate memory and not free it, it would stay allocated until you switched off or rebooted the computer, even after the process had ended. Memory leaks were an even more serious problem then.

查看更多
Rolldiameter
6楼-- · 2020-04-03 07:44

1) Long-running applications will run into problems, because the OS can only reclaim the memory when the application stops running.

2) delete b; also causes the destructor of the pointed-at B instance to run. Otherwise it will never run, since there is no longer any way to get at it. That destructor might do something important.

查看更多
我想做一个坏孩纸
7楼-- · 2020-04-03 07:53

It's certainly necessary the way you've written it. Even with the delete, though, the class is fundamentally broken because it manages a resource but doesn't follow the rule of three.

That said, there's almost certainly no reason for manual memory management--there rarely is. It's likely that you should either just have a B object as a member variable, or you should be using a smart pointer, like QScopedPointer:

struct A
{
    QScopedPointer<B> b;
    A() : b(new B()) { }

    // No ~A() needed; when the A object is destroyed, QScopedPointer ensures 
    // that the B object pointed to by the member 'b' is destroyed and deleted.
};

You'll want to make sure that you have a good introductory C++ book so that you can learn how to write correct C++ programs.

查看更多
登录 后发表回答