Comparison of C ++ memory objects

Source: Internet
Author: User
[Excerpt from: http://www.cnblogs.com/omygod/archive/2006/11/08/554601.html]

C ++ divides the memory into three logical regions:Heap, stack, and static storage Zone. In this case, the objects in them are stack objects, stack objects, and static objects.

Comparison of Three memory objects

Stack objects are automatically generated when appropriate and destroyed when appropriate.ProgramThe creation speed of stack objects is usually faster than that of heap objects, because operator new will be called when heap objects are allocated, and operator new will use some memory space to search.AlgorithmThe search process may be time-consuming, and it is not so troublesome to generate STACK objects. It only needs to move the top pointer of the stack. Note that the stack space is usually relatively small, usually 1 MB ~ 2 MB, so it is not suitable for allocating large objects in the stack.Note that it is best not to use stack objects in recursive functions.As the depth of recursive calls increases, the required stack space will also linearly increase. When the required stack space is insufficient, stack overflow will occur, resulting in running errors.
What will stack objects be automatically released? First, at the end of its life cycle; second, when its function is abnormal.

The Creation Time and destruction time of a heap object must be precisely defined by the programmer. That is to say, the programmer has full control over the life of the heap object. We often need such an object. For example, we need to create an object that can be accessed by multiple functions, but we do not want to make it global, at this time, creating a heap object is undoubtedly a good choice, and then passing the pointer of the heap object between each function can share the object. In addition, compared with stack space, the heap capacity is much larger. In fact, when the physical memory is not enough, if a new heap object needs to be generated at this time, it usually does not produce runtime errors, but the system will use virtual memory to expand the actual physical memory.

Next let's take a look at the static object.

The first is the global object. Global Objects provide the simplest way for inter-class communication and inter-function communication, although this method is not elegant. Generally, in a fully object-oriented language, there are no global objects, such as C #, because global objects mean insecure and highly coupled, excessive use of Global Objects in programs will greatly reduce program robustness, stability, maintainability and reusability. C ++ can also completely remove global objects, but it does not end up. I think one of the reasons is to be compatible with C.

The second is the static member of the class. As mentioned above, all objects in the base class and its derived class share this static member object, therefore, when data is shared or communicated between these classes or between these class objects, such static members are undoubtedly a good choice.

A static local object is used to save the intermediate state of the function where the object is located during repeated calls. One of the most notable examples is a recursive function, we all know that recursive functions call their own functions,If a nonstatic local object is defined in a recursive function, the overhead is huge when the number of recursion times is large.This is because the nonstatic local object is a stack object. Every recursive call will generate such an object. Every time this object is returned, it will be released, such an object is only limited to the current call layer. It is invisible to the deeper nested layer and the more exposed outer layer. Each layer has its own local object and parameters.

In recursive function design, static objects can be used to replace nonstatic local objects (stack objects). This not only reduces the overhead of generating and releasing nonstatic objects during each recursive call and return, static objects can also save the intermediate state of recursive calls and can be accessed by each call layer.

Related Article

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.