Why dynamic memory allocation
Array is a common data structure, but it has a disadvantage that the size of the array must be determined when it is used. If we want to use an array to save the data, what should we do? Setting the Array too large is a waste of space. If it is too small, it cannot be loaded. this is a situation. the object is too large. most of the data we use is stored in the stack by default, which is managed by the system and automatically allocated memory and deleted. but the stack is very small, just a few mb. If you read dozens of MB of text and save it to a string, the stack will surely be cracked.
The two most common scenarios are described above. in addition, if you want to control the release of memory accurately. for example, the memory is short, and you want to release it immediately after using a piece of memory. if the system automatically releases a variable, it may have to wait until the lifecycle of the variable ends. will not be immediately released.
Based on the above reasons, a heap occurs. This is a memory area controlled by the user, which is much larger than the stack. You can freely apply for a space to release the space.
Dynamic Memory Allocation in C Language
The C language is similar to the underlying layer. It is easier to understand the allocation of dynamic memory. C ++ is encapsulated to a certain extent. let's take an array as an example. if you do not know how much data you want to use, you only need to know it during running. N indicates that N int-type spaces are required. so we need to dynamically allocate a piece of memory and use an int pointer to point to the first address of the memory.
Int n = 123; // assign a value here. In actual use, it may be determined by passing a parameter.
Int * p = (int *) malloc (sizeof (int) * n );
/* Malloc is a library function that calls it to apply for memory space. Its return value is the void * pointer. Therefore, you need to convert the type to int * pointer. its parameter is the memory size, in bytes, indicating the number of bytes to be applied. int types may occupy different bytes in different systems. Therefore, use sizeof to calculate first. if you want to allocate memory to the struct, if there is a struct test, it is, struct test * pTest = (struct test *) malloc (sizeof (struct test ));*/
Now we can use the memory as an array. In fact, it is not a real array, just a simulated pull.
Int * pArray = p; // use another pointer to point to p, Because p needs to keep the first address of the memory, so that it will be correctly released when the memory is released later.
* PArray = 123;
* (PArray + 1) = 456;
Free (p); // you can release the memory when it is used up.
P = 0; // Let the pointer point to a blank memory, which has the advantages of, for example, if you are not careful where and then free p will go wrong, however, if p points to the empty memory, repeated free times will not cause errors.
// Free (p) is not the memory for releasing the p pointer, but a large block following the first address. the address value it saves is still there, so the address value saved by p is printed after you free it.
C ++ dynamic memory allocation
C ++ has multiple class concepts, and a more important concept in the class is constructor. constructor cannot be called manually. It is automatically called when a class is instantiated. if malloc is used to dynamically allocate a block of memory to a class like C, the class will not call the constructor. therefore, a new keyword appears in C ++. When you use new dynamic memory, the constructor will be automatically called (I don't know how to implement it. finally, it is encapsulated as a new for us ). delete is used after release, and the Destructor is called.
For example, if there is a class Arwen
Class Arwen
{
Public:
Arwen (string str) {name = str ;}
String name;
~ Arwen (){};
}
Arwen weiwen ("csharp"); // This class is instantiated. The system allocates memory in the stack and releases the memory.
Arwen * weiwenhp = new Arwen ("cplusplus"); // the pointer Arwen * must be used to allocate dynamic memory. You can apply for a space to release the space.
Delete weiwenhp; // release the memory.
Memory leakage
The most common mistake of dynamic memory allocation is memory leakage.
Strictly speaking, internal leakage is not an error. It just does not release the applied memory, resulting in a waste. in fact, it is easy to use this to create a virus. you keep applying for memory, but do not release it. in the end, the memory will be exhausted.
It is a complicated topic to study memory leakage. There are many cases that may cause leakage and there are many ways to prevent it.
Let's take a few simple examples.
Int * p = new int [88];
Delete p; // The memory is leaked. delete [] p is used. In C, free (p) is used.
In addition, if the delete operation in the function is not executed, it will also cause memory leakage. For example
Int function (int num)
{
Int * p = new [44];
If (num> 111)
Return 0;
Delete [] p;
Return 1;
}
If num is greater than 100, exit when return 0 is executed, and delete is not executed.