RAII programming thought of C + + learning

Source: Internet
Author: User
Background introduction

The allocation of resources is unavoidable in the process of running a C + + program-especially in the game! Resources can be in many, from textures, sounds, shader code to handles, and strings that can be called resources. The management of resources is an important round in the project, if not done well, then memory leaks, heavy memory crashes. RAII is an important programming idea for resource management in C + + projects.


First, a little C + +

The essential thing in C + + is class, and each class is indispensable to constructors and destructors. The former is used for a series of operations when an object is constructed, which is used for functions that are executed when the object is being refactored.


It is worth mentioning that in C + +, if a class is declared in the stack space, the class will automatically call the destructor after the function has finished executing from the stack space. However, if the display is declared in heap space (using the new method or the malloc method), you need to display the call to delete or the free function for destruction.


The above is to read this blog needs to understand the C + + knowledge, it should not be difficult ...


General concept

C + + has a lot of wonderful names, such as Yacc,raii, which is a long and proud tradition 2333333.


RAII is a typical example of "resource acquisition and initialization" (Resource aquisition is initialization), rather than some people think of "initialization as a resource acquisition" (Initialization is Resource Acquisition). BTW, if you want to be funny, blame the end, or not reach the effect.


RAII's technology is simple, using the concept of C + + object life cycle to control the resources of the program. Its technical rationale is simple, if you want to track an important resource, create an object and associate the life cycle of the resource with the life cycle of the object. In this way, C + + comes with an object management facility that can manage resources.


...


The above is the C + + learning RAII programming ideas content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.