object creation and time of existence

Source: Internet
Author: User
Tags garbage collection requires

From a technical standpoint, OOP (object-oriented programming) involves only abstract data types, inheritance, and polymorphism, but other issues may seem very important. These issues will be explored in this section.
One of the most important issues is the way objects are created and destroyed. Where is the data that the object needs, and how do you control the object's "presence Time"? To address this problem, the solution is different from the interesting. C + + considers the execution efficiency of a program to be the most important issue, so it allows the programmer to make a choice. To get the fastest speed, storage and time can be decided when writing a program, simply by placing the object on the stack (sometimes called an automatic or localized variable) or a static storage area. This provides a priority for allocation and release of storage space. In some cases, this kind of priority control is extremely valuable. However, we also sacrifice flexibility, because when writing a program, you must know the exact number, time, and type of the object. If a more conventional problem is to be solved, such as computer-aided design, warehousing management or air traffic control, this approach is too restrictive.
The second method is to create the object dynamically in a pool of memory, also known as "heap" or "memory heap." In this way, you do not know exactly how many objects are needed, or how long they exist, and exactly what type, unless you enter the runtime. These parameters are not determined until the program is fully operational. If you need a new object, simply create it in the memory heap when you need it. Because storage space is managed dynamically during run time, allocating storage space in the memory heap is much longer than it was created in the stack (creating storage space on the stack typically requires only a simple instruction to move the stack pointer down or down). Because the dynamic creation method makes the object inherently complex, the extra overhead of finding storage space and releasing it does not have a noticeable impact on the creation of the object. In addition, greater flexibility is essential for the resolution of conventional programming problems.
C + + allows us to decide whether to create an object while writing a program, or to create it during runtime, which is more flexible. You might think that, since it's so flexible, you should create objects in the memory heap, not on the stack anyway. But consider another problem, that is, the object's "presence Time" or "Time To Live" (Lifetime). If you create an object in the stack or in a static storage space, the compiler determines how long the object lasts and then automatically "destroys" or "clears" it. Programmers can use two ways to break an object: Programmatically deciding when to destroy an object, or using a "garbage collector" feature provided by a running environment to automatically look for objects that are no longer in use and purge them. Of course, the garbage collector appears much more convenient, but requires all applications to tolerate the presence of garbage collectors and to acquiesce to the extra overhead associated with garbage collection. But this does not conform to the C + + language design purposes, so could not be included in C + +. But Java does provide a garbage collector (Smalltalk also has this design; Although Delphi believes there is no garbage collector, you can choose to install it, and C + + can use some garbage collection products developed by other companies).
The remainder of this section discusses other factors to consider when manipulating objects.

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.