C++ Dynamic Memory
Understanding how dynamic memory works in C++ is essential to becoming a qualified C++ programmer. Memory in a C++ program is divided into two parts:
- Stack:All variables declared inside a function will occupy stack memory.
- Heap:This is memory not used by the program, which can be used for dynamic memory allocation during program execution.
Often, you cannot predict in advance how much memory is needed to store specific information in a defined variable; the required memory size can only be determined at runtime.
In C++, you can use special operators to allocate memory in the heap for variables of a given type at runtime, which returns the address of the allocated space. These operators are thenewOperators.
If you no longer need dynamically allocated memory space, you can usedeleteoperator to delete the memory previously allocated by the new operator.
new and delete operators
The following is the general syntax for using the new operator to dynamically allocate memory for any data type:
new data-type;
Here,data-typeIt can be any built-in data type, including arrays, or any user-defined data type, including classes or structures. Let's first look at built-in data types. For example, we can define a pointer to a double type, then request memory, which is allocated at execution time. We can use the following statement:newoperators to accomplish this:
If the free store has been exhausted, memory allocation may not succeed. Therefore, it is recommended to check whether the new operator returns a NULL pointer and take appropriate action as follows:
malloc()The malloc() function appeared in C language and still exists in C++, but it is recommended to avoid using malloc() whenever possible. Compared with malloc(), the main advantage of new is that new not only allocates memory, but also creates objects.
At any time, when you feel that a variable with dynamically allocated memory is no longer needed, you can use the delete operator to free the memory it occupies, as shown below:
delete pvalue; // 释放 pvalue 所指向的内存
The following example uses the above concept and demonstrates how to use the new and delete operators:
Example
When the above code is compiled and executed, it produces the following results:
Value of pvalue : 29495
Dynamic memory allocation for arrays
Suppose we want to allocate memory for a character array (a string with 20 characters). We can use the syntax from the above example to dynamically allocate memory for the array, as shown below:
char* pvalue = NULL; // 初始化为 null 的指针 pvalue = new char[20]; // 为变量请求内存
To delete the array we just created, the statement is as follows:
delete [] pvalue; // 删除 pvalue 所指向的数组
The following is the general syntax of the new operator that can allocate memory for multi-dimensional arrays, as shown below:
One-dimensional array
Two-dimensional array
2D array instance test:
Example
Three-dimensional array
3D array test instance:
Example
Dynamic memory allocation for objects
Objects are no different from simple data types. For example, look at the following code; we will use an array of objects to clarify this concept:
Example
If you want to allocate memory for an array containing four Box objects, the constructor will be called 4 times; similarly, when these objects are deleted, the destructor will also be called the same number of times (4 times).
When the above code is compiled and executed, it produces the following results:
Calling constructor! Calling constructor! Calling constructor! Calling constructor! Calling destructor! Calling destructor! Calling destructor! Calling destructor!other extensions