C + + dynamic memory allocation

Time:2022-5-1

The following essay is about C + + dynamic memory allocation.

Dynamic memory request operator new

  • new Type nameT(initialization parameter list)

  • Function:During the execution of the procedure, the application is used for storageTThe memory space of the type object, and the initial value is assigned according to the initial value list.

  • Result value:success:TPointer to the newly allocated memory; Failed: exception thrown.

Free memory operatordelete

  • delete Pointerp

  • Function:Release pointerpThe memory pointed to.pMust benewThe return value of the operation.

1 // example 1: dynamically creating objects
 2 
 3 #include 
 4 
 5 using namespace std;
 6 
 7 class Point {
 8 
 9 public:
10 
11 Point() : x(0), y(0) {
12 
13   cout<
1. Operation results:
 2 
 3 Step One:
 4 
 5 Default Constructor called.
 6 
 7 Destructor called.
 8 
 9 Step Two:
10 
11 Constructor called.
12 
13 Destructor called.

Allocating and releasing dynamic arrays

    • Allocation:new Type nameT [ Array length]

      • The array length can be any expression that is evaluated at run time

    • Release:delete[] Array namep

      • Release pointerpThe array pointed to.
        pMust be usednewThe first address of the allocated array.

1 // example 2: dynamically creating an object array
 2 
 3 #include
 4 
 5 using namespace std;
 6 
 7 class point {// the declaration of class is the same as example 6-16, omitted};
 8 
 9 int main() {
10 
11   Point *ptr = new Point[2]; // Create an array of objects
12 
13   ptr[0]. move(5, 10); // Accessing members of array elements through pointers
14 
15   ptr[1]. move(15, 20); // Accessing members of array elements through pointers
16 
17   cout << "Deleting..." << endl;
18 
19   delete[] ptr; // Delete the entire object array
20   
21   return 0;
22 
23 }
1. Operation results:
 2 
 3 Default Constructor called.
 4 
 5 Default Constructor called.
 6 
 7 Deleting...
 8 
 9 Destructor called.
10 
11 Destructor called.

Dynamically create multidimensional array

new Type nameT[The first1Dimension length][The first2Dimension length]…

  • If the memory request is successful,newThe operation returns a pointer to the first address of the newly allocated memory.

For example

  char (*fp)[3];

  fp = new char[2][3];

1 // example 3: dynamically create multidimensional arrays
 2 
 3 #include 
 4 
 5 using namespace std;
 6 
 7 int main() {
 8 
 9   int (*cp)[9][8] = new int[7][9][8];
10 
11   for (int i = 0; i < 7; i++)
12 
13     for (int j = 0; j < 9; j++)
14 
15       for (int k = 0; k < 8; k++)
16 
17         *(*(*(cp + i) + j) + k) =(i * 100 + j * 10 + k);
18 
19   for (int i = 0; i < 7; i++) {
20 
21     for (int j = 0; j < 9; j++) {
22 
23       for (int k = 0; k < 8; k++)
24 
25         cout << cp[i][j][k] << " ";
26 
27         cout << endl;
28 
29     }
30 
31     cout << endl;
32 
33   }
34 
35   delete[] cp;
36 
37   return 0;
38 
39 }

 

 

Recommended Today

JVM + GC parsing (premise knowledge concatenation)

Premise preparation JVM GC garbage collection JVM virtual machine monitoring, tuning and troubleshooting Tomcat and microservice optimization 1. Premise review 1.1. JVM memory structure 1.1.1、 JVM Architecture Overview The gray part in the figure isThread private, there is almost no garbage collectionOrange partThread sharing, the main place where garbage recycling occurs What is the class […]