C + + memory management in COCOS2D-X development

Source: Internet
Author: User

since there is no introduction to the C + + language at the beginning, the memory management of C + + does not have any explanation, in order to master the memory management mechanism in cocos2d-x, it is necessary to understand some C + + memory management knowledge first.
C + + memory management is very complex, and it can be explained clearly if you fully systematically introduce a book. Here you will find only the most basic usage of C + + memory management.


Memory allocation Area
Creating an object requires two steps: The first step is to allocate memory for the object, and the second step is to call the constructor to initialize the memory. When the object allocates memory in the first step, we can select several different allocation areas, which are as follows:
Stack area assignment. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity. It is automatically allocated and freed by the processor to hold the parameter values of the function and the values of the local variables. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end.
Heap area allocation. Allocated from the heap, also known as dynamic memory allocation. Released by the developer, if not released, is reclaimed by the operating system at the end of the program. When the program is running, it uses malloc or new to request any amount of memory, and the developer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by the developer and is very flexible to use, but the problem is the most.
Allocated in a static storage area. This memory space exists during the entire run of the program and is already allocated when the program is compiled. It can assign global variables and static variables.


Dynamic memory allocation
Dynamic memory allocation is the most flexible but there are many problems, and we focus on dynamic memory allocation. Dynamic memory allocates memory using malloc or new, freeing memory with free or delete. where malloc and free are paired, new and delete are paired.
1. malloc and free use
malloc and free are standard library functions in the C/C + + language, mainly used in C. Creating an object using malloc does not automatically invoke the constructor to initialize memory. Releasing an object with free does not automatically call the destructor to clear memory.
The instance code for allocating and freeing memory using malloc and free is as follows:
#include <iostream>using namespace Std;class MyObject  {  public:myobject () {①cout << "call constructor. "<< Endl; }  ~myobject () {②cout << "call destructor." << Endl;}  void Initialize () {③cout << "call initialization." << Endl;}  void Destroy () {④cout << "call Destroy." << Endl;}  ;  int main () {MyObject *obj = (MyObject *) malloc (sizeof (MyObject));//Request Dynamic Memory  ⑤obj->initialize ();⑥//todoobj-> Destroy (); ⑦free (obj); ⑧obj = Null;return 0;}


The code above creates a declaration of the MyObject class, where the ① line code is the Declaration constructor, and the ② Line code is the declaration destructor. The ③ Line code is the declaration of the initialization function void Initialize () when allocating memory using malloc, the constructor cannot be called, and the object is initialized by calling the function. The ④ Line code is a declaration that clears the function void destroy () when using free to dispose of object memory by calling the function to clear some resources for that object.
Line ⑤~⑧ is called the MyObject class for testing where the ⑤ line code MyObject *obj = (MyObject *) malloc (sizeof (MyObject)) is allocating memory using the malloc function, which requires the length of the specified object, There is also the malloc function return value is void*, because C + + does not allow void* to be assigned to other pointers, you need to force the type conversion. Because the constructor cannot be called explicitly, the ⑥ line code is required to initialize the object.
The ⑧ Line code free (obj) is the release of the Obj object memory. Before releasing the object memory, we call the ⑦ Line code Obj->destroy () to clear some resources of the object before releasing the object memory, which is equivalent to the destructor. But the real destructor ~myobject () is not called.
The results of the operation are as follows:
Call initialization.
Call Destroy.


2, new and delete use
Unlike malloc and free, new and delete are not function libraries, but C + + operators. The new operator is able to complete all steps of creating an object (that is, the first step, allocating memory for the object, the second step, calling the constructor to initialize the memory), and it will also call the constructor. Instance code:
MyObject *obj = new MyObject ();
The constructor can be overloaded, depending on the parameter list passed by the user, deciding which constructor to invoke to initialize the object.
The new operator anti-operator is a delete,delete that first calls the destructor and then frees the memory. Instance code:
Delete obj;
OBJ is an object pointer, and obj can only release objects created by new and cannot be freed with malloc creation. And with the delete-freed object pointer, you need to obj=null to prevent "wild pointers".
The hint is that the pointer variable is not initialized, its point is random, it is not null. If the IF statement is used, it is considered a valid pointer. The other thing is that after the pointer variable is free or delete, they simply release the memory that the pointer refers to, but do not clear the pointer itself, when the pointer is pointing to "garbage" memory. It is also considered a valid pointer if it is judged by the IF statement. The "Wild pointer" is dangerous, and good programming practice is that both cases need to set the pointer to null, which is the only way to avoid "wild pointers".
The instance code for allocating and freeing memory using new and delete is as follows:
#include <iostream>using namespace Std;class MyObject  {  public:myobject () {cout << "call constructor. "<< Endl; }  ~myobject () {cout << "call destructor." << Endl;}  void Initialize () {cout << "call initialization." << Endl;}  void Destroy () {cout << "call Destroy." << Endl;}  ;  int main () {MyObject *obj = new MyObject ();//Request Dynamic memory  


The same is the MyObject class, which uses new to allocate memory, and delete to free memory. The program runs calls constructors and destructors. The results of the operation are as follows:
Call constructor.

Call destructor.


For more information, please pay attention to the first Cocos2d-x 3.2 Edition book "Cocos2d-x: C + + volume" book Exchange Discussion website: http://www.cOcoagame.net
For more exciting video courses, please follow Cocos class: http://v.51wOrk6.com
Welcome to join Cocos2d-x Technical Discussion group: 257760386Welcome to Luxgen iOS Classroom public platform

C + + memory management in COCOS2D-X development

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.