C++ dynamic memory
- 2020-10-07 18:47:54
- OfStack
Understanding how dynamic memory works in C++ is essential to becoming a qualified C++ programmer. The memory in the C++ program is divided into two parts:
Stack: All variables declared within a function occupy stack memory. Heap: Unused memory in a program that can be used to dynamically allocate memory while the program is running.Many times, you cannot predict in advance how much memory is needed to store specific information in a defined variable, and the amount of memory required needs to be determined at run time.
In C++, you can use special operators to allocate memory in the heap at run time for a variable of a given type, which returns the allocated spatial address. This operator is the new operator.
If you no longer need dynamically allocated memory space, you can use the delete operator and delete the memory previously allocated by the new operator.
The new and delete operators
Here is the general syntax for using the new operator to dynamically allocate memory for arbitrary data types:
[new data-type;
]In this case, ES30en-ES31en can be any built-in data type, including arrays, or any user-defined data type, including classes or structures. Let's look at the built-in data types first. For example, we could define a pointer to the type double and then request memory that exists and is allocated at execution time. We can do this using the new operator as follows:
double* pvalue = NULL; // Initialized to null A pointer to the
pvalue = new double; // Request memory for variables
If free storage is used up, memory may not be allocated successfully. It is recommended to check that the new operator returns the NULL pointer and do the following as appropriate:
double* pvalue = NULL;
if( !(pvalue = new double ))
{
cout << "Error: out of memory." <<endl;
exit(1);
}
The malloc() function has been available in the C language and still exists in C++, but malloc() functions are recommended to be avoided whenever possible. The main advantage of new over the malloc() function is that new does not just allocate memory, it also creates objects.
At any time when you feel that a variable that has been dynamically allocated memory no longer needs to be used, you can use the delete operator to free the memory it occupies, as shown below:
delete pvalue; // The release of pvalue The memory being pointed to
The following example USES the above concepts and demonstrates how to use the new and delete operators:
The instance
#include <iostream>
using namespace std;
int main ()
{
double* pvalue = NULL; // Initialized to null A pointer to the
pvalue = new double; // Request memory for variables
*pvalue = 29494.99; // Stores a value at the assigned address
cout << "Value of pvalue : " << *pvalue << endl;
delete pvalue; // Free memory
return 0;
}
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 1-character array (a 20-character string), we can use the syntax in the example above to dynamically allocate memory for an array, as shown below:
char* pvalue = NULL; // Initialized to null A pointer to the
pvalue = new char[20]; // Request memory for variables
To delete the array we just created, the statement reads as follows:
delete [] pvalue; // delete pvalue The array to which
Here is the general syntax for the new operator, which allocates memory for multidimensional arrays, as follows:
1 d array
// Dynamic allocation , The array length is m
int *array=new int [m];
// Free memory
delete [] array;
2 d array
int **array
// Assume array control 1 Dimension of length m . The first 2 Dimension of length n
// Dynamic allocation space
array = new int *[m];
for( int i=0; i<m; i++ )
{
array[i] = new int [n] ;
}
// The release of
for( int i=0; i<m; i++ )
{
delete [] array[i];
}
delete [] array;
2 - dimensional array instance test:
The instance
#include <iostream>
using namespace std;
int main()
{
int **p;
int i,j; //p[4][8]
// Assigned starting 4 line 8 The column 2 D data
p = new int *[4];
for(i=0;i<4;i++){
p[i]=new int [8];
}
for(i=0; i<4; i++){
for(j=0; j<8; j++){
p[i][j] = j*i;
}
}
// Print data
for(i=0; i<4; i++){
for(j=0; j<8; j++)
{
if(j==0) cout<<endl;
cout<<p[i][j]<<"\t";
}
}
// Start releasing the stack of applications
for(i=0; i<4; i++){
delete [] p[i];
}
delete [] p;
return 0;
}
3 d array
int ***array;
// Assume array control 1 D for m . The first 2 D for n . The first 3 D for h
// Dynamic allocation space
array = new int **[m];
for( int i=0; i<m; i++ )
{
array[i] = new int *[n];
for( int j=0; j<n; j++ )
{
array[i][j] = new int [h];
}
}
// The release of
for( int i=0; i<m; i++ )
{
for( int j=0; j<n; j++ )
{
delete[] array[i][j];
}
delete[] array[i];
}
delete[] array;
3d array test example:
The instance
double* pvalue = NULL;
if( !(pvalue = new double ))
{
cout << "Error: out of memory." <<endl;
exit(1);
}
0
Dynamic memory allocation for objects
Objects are no different than simple data types. For example, look at the following code, we will use an array of objects to clarify the concept of 1:
The instance
double* pvalue = NULL;
if( !(pvalue = new double ))
{
cout << "Error: out of memory." <<endl;
exit(1);
}
1
If you want to allocate memory for an array of four Box objects, the constructor will be called four times, and likewise, the destructor will be called the same number of times (four) when these objects are deleted.
When the above code is compiled and executed, it produces the following results:
[
Call the constructor!
Call the constructor!
Call the constructor!
Call the constructor!
Call the destructor!
Call the destructor!
Call the destructor!
Call the destructor!
The above is 1 article to understand C++ dynamic memory details, more C++ dynamic memory information please pay attention to other related articles on this site!