C ++ atmiņas pārvaldība: jauna un dzēsta

Šajā apmācībā mēs iemācīsimies efektīvi pārvaldīt atmiņu C ++, izmantojot jaunas un dzēšamas darbības, izmantojot piemērus.

C ++ ļauj mums piešķirt mainīgā vai masīva atmiņu izpildes laikā. To sauc par dinamisko atmiņas piešķiršanu.

Citās programmēšanas valodās, piemēram, Java un Python, kompilators automātiski pārvalda mainīgajiem piešķirtās atmiņas. Bet tas nav gadījumā ar C ++.

Programmā C ++ dinamiski piešķirtā atmiņa ir jānokārto manuāli pēc tam, kad mainīgais nav izmantots.

Mēs varam dinamiski piešķirt un pēc tam sadalīt atmiņu, attiecīgi izmantojot newun deleteoperatorus.

C ++ jauns operators

newOperatoram piešķir atmiņu mainīgajam. Piemēram,

 // declare an int pointer int* pointVar; // dynamically allocate memory // using the new keyword pointVar = new int; // assign value to allocated memory *pointVar = 45;

Šeit mēs dinamiski esam piešķīruši intmainīgajam atmiņu, izmantojot newoperatoru.

Ievērojiet, ka mēs esam izmantojuši pointer pointVar, lai dinamiski piešķirtu atmiņu. Tas ir tāpēc, ka newoperators atgriež atmiņas vietas adresi.

Masīva gadījumā newoperators atgriež masīva pirmā elementa adresi.

No iepriekš minētā piemēra mēs varam redzēt, ka newoperatora izmantošanas sintakse ir

 pointerVariable = new dataType;

dzēst operatoru

Kad mums vairs nav jāizmanto mainīgais, kuru esam deklarējuši dinamiski, mēs varam sadalīt mainīgā aizņemto atmiņu.

Šim nolūkam deletetiek izmantots operators. Tas atgriež atmiņu operētājsistēmā. To sauc par atdalīšanu atmiņā .

Šī operatora sintakse ir

 delete pointerVariable;

Apsveriet kodu:

 // declare an int pointer int* pointVar; // dynamically allocate memory // for an int variable pointVar = new int; // assign value to the variable memory *pointVar = 45; // print the value stored in memory cout << *pointVar; // Output: 45 // deallocate the memory delete pointVar;

Šeit mēs dinamiski esam piešķīruši intmainīgajam atmiņu, izmantojot pointer pointVar.

Pēc pointVar satura izdrukāšanas mēs sadalījām atmiņu, izmantojot delete.

Piezīme . Ja programma izmanto lielu daudzumu nevēlamas atmiņas new, sistēma var avarēt, jo operētājsistēmai nebūs pieejama atmiņa. Šajā gadījumā deleteoperators var palīdzēt sistēmai no avārijas.

1. piemērs: C ++ dinamiskās atmiņas piešķiršana

 #include using namespace std; int main() ( // declare an int pointer int* pointInt; // declare a float pointer float* pointFloat; // dynamically allocate memory pointInt = new int; pointFloat = new float; // assigning value to the memory *pointInt = 45; *pointFloat = 45.45f; cout << *pointInt << endl; cout << *pointFloat << endl; // deallocate the memory delete pointInt, pointFloat; return 0; )

Rezultāts

 45 45.45

Šajā programmā mēs dinamiski iedalījām atmiņu diviem mainīgajiem intun floattipiem. Pēc tam, kad viņiem ir piešķirtas vērtības un tās ir izdrukātas, mēs beidzot sadalām atmiņas, izmantojot kodu

 delete pointInt, pointFloat;

Piezīme: Dinamiska atmiņas piešķiršana var padarīt atmiņas pārvaldību efektīvāku.

Īpaši masīviem, kur daudzas reizes mēs nezinām masīva lielumu līdz izpildes laikam.

2. piemērs: C ++ jauns un izdzēsiet Operatoru masīviem

 // C++ Program to store GPA of n number of students and display it // where n is the number of students entered by the user #include #include using namespace std; int main() ( int num; cout <> num; float* ptr; // memory allocation of num number of floats ptr = new float(num); cout << "Enter GPA of students." << endl; for (int i = 0; i < num; ++i) ( cout << "Student" << i + 1 <> *(ptr + i); ) cout << "Displaying GPA of students." << endl; for (int i = 0; i < num; ++i) ( cout << "Student" << i + 1 << " :" << *(ptr + i) << endl; ) // ptr memory is released delete () ptr; return 0; )

Rezultāts

Ievadiet kopējo studentu skaitu: 4 Ievadiet studentu GPA. Student1: 3.6 Student2: 3.1 Student3: 3.9 Student4: 2.9 Parāda studentu GPA. Students1: 3,6 Students2: 3,1 Students3: 3,9 Students4: 2,9

Šajā programmā mēs esam lūguši lietotāju ievadīt studentu skaitu un saglabāt to mainīgajā skaitlī.

Tad mēs floatmasīvam esam dinamiski piešķīruši atmiņu, izmantojot jaunu.

Mēs ievadām datus masīvā (un vēlāk tos izdrukājam), izmantojot rādītāju apzīmējumus.

Pēc tam, kad masīvs vairs nav vajadzīgs, masīva atmiņa tiek sadalīta, izmantojot kodu delete () ptr;.

Ievērojiet ()pēc delete. Mēs izmantojam kvadrātiekavas (), lai apzīmētu, ka atmiņas sadalījums ir masīvam.

3. piemērs: C ++ jauns un dzēsiet objektu operatoru

 #include using namespace std; class Student ( int age; public: // constructor initializes age to 12 Student() : age(12) () void getAge() ( cout << "Age = " << age 

Output

 Age = 12

In this program, we have created a Student class that has a private variable age.

We have initialized age to 12 in the default constructor Student() and print its value with the function getAge().

In main(), we have created a Student object using the new operator and use the pointer ptr to point to its address.

The moment the object is created, the Student() constructor initializes age to 12.

We then call the getAge() function using the code:

 ptr->getAge();

Notice the arrow operator ->. This operator is used to access class members using pointers.

Interesanti raksti...