C++ is it necessary to delete dynamically allocate

2019-05-08 06:18发布

When using dynamically allocated objects in C++ eg:

TGraph* A = new TGraph(...);

One should always delete these because otherwise the objects might still be in memory when control is handed back to the parent scope. While I can see why this is true for subscopes and subroutines of a program, does the same count for the main scope?

Am I obliged to delete objects that were dynamically built inside main()? The reason why this seems a bit redudant to me is that when main ends, the program also ends, so there is no need to worry about memory leaks.

7条回答
▲ chillily
2楼-- · 2019-05-08 06:44

The reason why this seems a bit redudant to me is that when main ends, the program also ends, so there is no need to worry about memory leaks.

You're right, but consider this: you create a class object which opens a connection to remote DB. After your program will complete, you should tell the DB "I'm done, i'm going to disconnect", but it won't happen in the case you won't call delete properly.

查看更多
我只想做你的唯一
3楼-- · 2019-05-08 06:45

Yes, you should call delete, at least because it's best practice. If you have important logic in your destructor, that's one extra reason that you should call delete.

Corrected: If the program depends on logic in the destructor, not calling delete explicitly results in undefined behavior.

查看更多
可以哭但决不认输i
4楼-- · 2019-05-08 06:50

Most of the modern OS always reclaim back all memory they allocated to a program(process).
The OS doesn't really understand if your program leaked memory it merely takes back what it allocatted.

But there are bigger issues at hand than just the memory loss:

Note that if the destructor of the object whos delete needs to be called performs some non-trivial operation and your program depends on the side effects produced by it then your program falls prey to Undefined Behavior[Ref 1]. Once that happens all bets are off and your program may show any beahvior.

Also, An OS usually reclaims the allocated memory but not the other resources, So you might leak those resources indirectly. This may include operations dealing with file descriptors or state of the program itself etc.

Hence, it is a good practice to always deallocate all your allocations by calling delete or delete [] before exiting your program.


[Ref 1]C++03 Standard 3.8 Para 4:

"....if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior."

查看更多
Summer. ? 凉城
5楼-- · 2019-05-08 06:55

Its the best practice to de-allocate memory that's been allocated. You should keep in mind that Heap memory is limited and just allocating with out de-allocating while your program is running might run the heap space out for some other/or the same program(if its some kind of a daemon that is meant to run for a very long time) that needs heap.

Of course memory will be reclaimed by the operating system at the end of the program's execution.

查看更多
Luminary・发光体
6楼-- · 2019-05-08 07:03

I see you are using ROOT (CMS guy?). I think ROOT takes care of this and cleans up, doesn't it?

查看更多
smile是对你的礼貌
7楼-- · 2019-05-08 07:05

Best practices:

  1. Do not use new, use automatic allocation
  2. When dynamic allocation is necessary, use RAII to ensure automatic cleanup

You should never have to write delete in applicative code.

Here, why are you calling new for TGraph ?

TGraph A(...);

works better: less worries!

查看更多
登录 后发表回答