Introduction to C++ (5) : new and delete

  • 2020-04-02 02:59:12
  • OfStack

For those of you who haven't been exposed to C++ before, and who have been exposed to cocos2d-x for the first time, you may be unfamiliar with memory management.
I also often cause various minor bugs due to memory problems. I also wrote an article about how to play with retain and release. , the object reference and release for handling cocos2d-x is sufficient.
But don't you want to know the secrets behind retain and release? (xiao ruo: no.)
 
Yes, today wood to take you into science, into the world, together to discuss C++ of new and delete. (xiao xiao: no.)
 
Well, since everyone can't wait, let's get started

1. Dynamically allocate memory

We all know, like "int num = 10;" This statement declares an num variable that needs to be stored in memory (just as your document needs to be stored on a hard disk).
For such ordinary variables, memory is allocated at compile time.
Yeah, like when you were born you decided whether you were a boy or a girl. Small if: this analogy feeling a bit relation all pull not top! And who says it's determined at birth? !).
 
By declaring pointer variables, we can point to these pre-allocated memory addresses, but our Pointers can exist for more than that.
Pointers can also hold the addresses of dynamically allocated memory.
So, how do you allocate memory dynamically? That's right, new, as follows:


//You can do this
int *p = new int;
//You can also do
int *p = new int();
//Use the < br / > *p = 20;
//Finally, free memory
delete p;

With new followed by a type, you can create a memory space to hold a certain type, and then return the address of that memory space.
The difference between this and declaring an int is:
1. The new variable will only apply for memory when running the program, and the ordinary int variable will allocate memory when compiling.
2. The new variable needs to be released when it is not used, otherwise the memory will leak. The memory space pointed to by pointer can be released with delete.

2. Pairing of new and delete

Dynamic application of memory, when not in use, must not forget to release, otherwise memory leak.
To put it crudely, no, to put it crudely, dynamically requesting memory simply tells the operating system that the memory is mine and no one else can use it.
The operating system will obediently give you the memory you requested, and if you don't explicitly tell it that you don't need it, it will always be yours to use. (of course, there will be accidents, this is ignored.)
 
So, never forget to delete out when not in use.
Whenever you create a new variable, there must be a corresponding delete.

3. Relationship between new and delete and reatin and release

Now let's look at cocos2d-x memory management, which was created so that we can ignore the pairing of new and delete.
Who has nothing to want to remember every day where he is new, and where he forgot delete?
 
So retain and release were born.
Most of the objects in cocos2d-x are created using the create function, which does two main things:
1. Call new to create a new object
2. Add objects to the memory management pool (I won't go into the details of reference counting rules)
 
One of the main things that cocos2d-x's memory management does is:
1. Check all objects that participate in memory management, and call delete on those that need to be freed to free memory
 
Therefore, we don't need to maintain new and delete by ourselves. When we create an object, we just hand it over to memory management.
If we do not call retain, then the object will be released at the next memory management check (the next frame).
Meanwhile, functions such as addChild will actively call the retain function of the object once, so objects that are addChild will not be released.
When leaving the scene and other operations, the object will also be called the release function to offset the effect of a retain.
 
We don't need to actively call the retain function unless necessary, which is the basic rule of "automatic memory management."

4. Dynamic array

In addition to dynamically creating variables, arrays can also be created dynamically: int *nums = new int[10];
Instead, releasing dynamic arrays is a bit special: delete [] nums;
After the delete, you need to add a [] to represent the array being released.
 
Dynamic arrays are used in much the same way as regular arrays, with minor differences, of course:


 int *nums = new int[3];
    nums[0] = 1;
    nums[1] = 2;
    nums[3] = 3;
    cout << nums[0];
    nums += 1;
    cout << nums[0];

The first time nums[0] is printed using cout, it outputs the value of the first element: 1.
However, when nums += 1 is called, the pointer to nums is already pointing to the next address, which is the address of nums[1].
So, if I call nums[0] again, I'm going to print the value of the first element, but the first element is no longer 1, it's 2.

End of the 5.

Okay, so I'm going to go over here for new and delete.
However, there is still a small part of the preliminary introduction to Pointers, which will be covered in the next article


Related articles: