Is garbage collection automatic in standard C++?

2019-01-18 04:17发布

From what I understand, in standard C++ whenever you use the new operator you must also use the delete operator at some point to prevent memory leaks. This is because there is no garbage collection in C++. In .NET garbage collection is automatic so there is no need to worry about memory management. Is my understanding correct? Thanks.

8条回答
Summer. ? 凉城
2楼-- · 2019-01-18 05:03

Your statement about operator new is totally correct...but it's oversimplifying C++ semantics quite a bit.

In C++, objects can be created on the stack or on the heap:

class Foo {};

int main() {
  Foo obj1;
  Foo* obj2 = new Foo();
  delete obj2;
}

In the above example, obj1 is created on the stack and obj2 is created on the heap (with new). Objects created on the heap are not destroyed until delete is explicitly called on them. However, objects on the stack are automatically destroyed when they go out of scope (i.e. when main() returns in this example).

This enables the "Resource Acquisition Is Initialization" idiom (a.k.a. RAII) in C++, which is much more powerful than basic garbage collection. Resources that need to be cleaned up (heap memory, sockets, files, DB connections, etc.) are generally put in stack-based objects whose destructors take care of cleanup.

In contrast, Java and C# don't allow objects to be constructed on the stack, and do not guarantee that collection will ever happen nor that finalizers will run (I'm not a C# guy, so I may be a little wrong there). So while you get free heap memory management in Java/C#, you'll actually end up with a lot more resource cleanup code in those languages than you do in C++.

查看更多
在下西门庆
3楼-- · 2019-01-18 05:04

In idiomatic high-level C++, you never call delete.

C++ does not have a standard garbage collector that works the same as in C#, and therefore it is true that fundamentally, new and delete need to be paired. However, there are mechanisms in C++ that completely eliminate the explicit use of delete for code written in the modern style.

The first thing to note is that in C++ you use new much less frequently than you use new in C#. This is because in C# you use new whenever you create an instance of a structure, class, or array, but in C++, you use new only when you want to manage a data element dynamically. Most data in C++ does not require dynamic management and can therefore be created without the use of new. [Put another way, new has a different meaning in C# than in C++. In C++ it specifically indicates dynamic allocation, while in C# it is used for any construction.]

Secondly, any time you do call new in C++, the return value should be handed directly to a smart pointer. The smart pointer will ensure that delete is automatically called for you at the appropriate time.

By the way, unless you are a guru writing a low-level library (or a student learning how to do this), you should never call new to allocate an array in C++. The standard library (and also Boost/TR1) provide template classes that allocate and manage arrays for you.

In summary, C++ does not use a garbage collector but it does have its own form of automatic memory management. There are subtle differences between the two approaches, but both approaches automate the release of memory, thereby eliminating most types of memory leaks.

The authoritative demonstration of these concepts is given by C++ creator Bjarne Stroustrup in answer to the question: How do I deal with memory leaks?

See also:

查看更多
登录 后发表回答