Waste collection defects in c ++

Source: Internet
Author: User

I have made a lot of effort to study the collection of waste in C ++, here, I have summarized my own experience in collecting waste in C ++. Here I will study it with you. It is a mechanism, it can simplify the design and eliminate many root causes of errors.

In this article, I do not want to expose the inherent defects of the waste collection mechanism provided by Java, but point out the feasibility of introducing waste collection in C ++. Please note that the methods described here are more based on the current standards and Library Design, rather than requiring to modify the language definition or extend the compiler.

What is waste collection?
As a programming language that supports pointers, C ++ delivers the convenience of dynamically managing memory resources to programmers. When using pointer-based objects (note that since the reference cannot change the language mechanism restriction of the reference target after initialization, The polymorphism application depends on the pointer in most cases ), programmers must allocate, use, and release their own memories. The language itself cannot provide any help in this process. Maybe, apart from working closely with the operating system according to your requirements, complete the actual memory management. "Undefined" is mentioned many times in the standard text, which is related to pointers in most cases.

  • Detailed introduction to the C ++ language and its Code for learning
  • Detailed introduction to writing C ++ code that is easy to understand
  • Describes the problems and skills of the C ++ Language
  • Exploring various concise and flexible features of C ++
  • How to Use the Visual C ++ subset to search for a topic

Some languages provide waste collection mechanisms. That is to say, programmers are only responsible for allocating and using memory, while the language itself is responsible for releasing memory that is no longer in use, in this way, the programmer will get away from the annoying memory management work. However, C ++ does not provide a similar mechanism, bjarne Stroustrup, a C ++ designer, is The only book I know about language Design, The Design and Evolution of C ++: the Design and evolution of the C ++ language is described in a section. In short, Bjarne thinks.

"I intended to design C ++ in this way so that it does not rely on automatic waste collection (generally speaking, waste collection ). This is based on my own experience with the waste collection system. I am afraid of the serious space and time overhead and the complexity of implementing and porting the waste collection system. In addition, waste collection will make C ++ unsuitable for a lot of underlying work, but this is exactly one of its design goals. However, I like the idea of collecting waste in C ++. It is a mechanism that simplifies the design and eliminates many root causes of errors.

The basic reason for waste collection is easy to understand: user convenience and more reliable than the storage management mode provided by users. There are also many reasons to oppose waste collection, but they are not the most fundamental, but about implementation and efficiency.

There are already many arguments to refute: each application will do better after collection of waste. Likewise, there are sufficient arguments to oppose: No application may be able to do better with waste collection. Not every program needs to run endlessly. Not all code is a basic library code;

For many applications, it is acceptable to have a slight loss of storage. Many applications can manage their own storage without the need for waste collection or other related technologies, such as reference counting. In principle and feasibility, waste collection is required. However, for today's users and general use and hardware, we cannot afford to define the semantics of C ++ and its basic library on the waste collection system ."

In my opinion, the Unified Automatic waste collection system cannot be applied to different application environments without causing implementation burden. Later, I will design an optional waste collector for a specific type. Obviously, there are more or less efficiency overhead. If the C ++ user is forced to accept this, it may not be desirable.

The books mentioned above are the most comprehensive explanation of why C ++ has no waste collection and may make efforts in C ++, although there is only one short section, it has already covered a lot of content. This is exactly what Bjarne has always been like. It is concise and full of rhyme.

Next, we will introduce the waste collection system of our local wines step by step, which can be freely selected as needed without affecting other codes, the constructor and destructor provided in C ++ can well meet the demand for Automatic Resource release. Bjarne has a famous saying: "Resource requirement Is Initialization )".

Therefore, we can apply for the resources to be allocated in the constructor, and release the allocated resources in the Destructor as long as the object's lifetime ends, the resources allocated by the request for collection objects in C ++ are automatically released. There is only one problem left. If the object itself is dynamically created in a Free Store (the so-called "Heap, and managed by pointers (I believe you already know why), you must call the Destructor explicitly through encoding, and use the delete expression of the pointer, of course.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.