Dynamic memory in C ++
An in-depth understanding of how dynamic memory really works in C / C ++ is crucial to being a good C / C ++ programmer. Memory in your C / C ++ program is divided into two parts:
- Stack : All variables declared inside the function will receive memory from the stack in C / C ++.
- Heap : Used to allocate dynamic memory when the program runs.
Sometimes, you don't know in advance how much memory you will need to store specific information in a defined variable and the required memory size can be determined at run time.
You can allocate memory at run time inside the Heap for that variable with a given type using a special operator in C / C ++ that returns the address of the allocated space. This operator is called the new operator in C / C ++.
If you don't need dynamic memory allocated anymore, you can use the delete operator in C / C ++, which frees the memory that was previously allocated by the new operator.
New and delete operators in C / C ++
This is a common syntax to use the new operator to allocate dynamic memory for any data type in C / C ++:
new kieu_du_lieu ;
Here, kieu_du_lieu can be any existing data type such as an array or defined data types such as classes or structures. First, we consider the available data types. For example, we can define a pointer to type double and then ask that memory be allocated at runtime. We can do this by using the new operator in C / C ++ with the following commands:
double * contro = NULL ; // con tro duoc khoi tao voi gia tri null contro = new double ; // yeu cau bo nho cho bien
The memory may not have been allocated successfully, if the free store is already in use. Therefore, this is a good practice for you to check if the new operator returns NULL and performs the appropriate action, as follows:
double * contro = NULL ; if ( !( contro = new double )) { cout << "Error: Het bo nho!" << endl ; exit ( 1 ); }
The malloc () function from C, still exists in C / C ++, but I suggest you avoid using this malloc () function. The biggest advantage of the new operator compared to malloc () is that the new operator not only allocates memory, it also builds the object according to the main purpose of C / C ++.
At any time, when you feel a dynamically allocated variable is no longer needed, you can free up memory that it has captured in the cache section with the delete operator in C / C ++, like after:
delete contro ; // giai phong bo nho duoc tro boi contro
Now, based on the above concepts, we create an example to illustrate how new and delete operators in C / C ++ work:
#include using namespace std ; int main () { double * contro = NULL ; // con tro duoc khoi tao voi gia tri null contro = new double ; // yeu cau bo nho cho bien * contro = 1234.56 ; // luu giu gia tri tai dia chi da duoc cap phat cout << "Gia tri cua contro la: " << * contro << endl ; delete contro ; // giai phong bo nho. return 0 ; }
Compiling and running the above C / C ++ program will produce the following results:
Gia tri cua contro la : 1234.56
Allocate dynamic memory for Arrays in C / C ++
Suppose you want to allocate memory for an array of characters, for example a string of 20 characters. You use the same syntax used above, as follows:
char * contro = NULL ; // con tro duoc khoi tao voi gia tri null contro = new char [ 20 ]; // yeu cau bo nho cho bien
To delete the array we just created, the syntax in C / C ++ is:
delete [] contro ; // xoa mang da duoc tro boi contro
This is the general syntax of the new operator, which you can use to allocate memory for a multidimensional array in C / C ++:
double ** contro = NULL ; // con tro duoc khoi tao voi gia tri null contro = new double [ 3 ][ 4 ]; // cap phat bo nho cho mang 3x4
However, the syntax for freeing memory for multidimensional arrays remains the same:
delete [] contro ; // xoa mang da duoc tro boi contro
Allocate dynamic memory for Objects in C / C ++
The object is no different from simple data types. For example, consider the following code, in which we are using an object array to clarify this concept:
#include using namespace std ; class NhanVien { public : NhanVien () { cout << "Constructor duoc goi!" << endl ; } ~ NhanVien () { cout << "Destructor duoc goi!" << endl ; } }; int main ( ) { NhanVien * mangNhanVien = new NhanVien [ 5 ]; delete [] mangNhanVien ; // xoa mang return 0 ; }
If you are about to allocate memory for an array of 5 objects, the above constructor will be called 4 times and the same while deleting the objects and destructors will be called with the same number of times.
Compiling and running the above C / C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Exception handling (Exception Handling) in C ++
Next article: Namespace in C ++
You should read it
- Arrays and objects in JavaScript are like stories and newspapers!
- How to delete objects and objects in Photoshop
- Array data structure
- Memory management in C
- Operator in PHP
- Boolean operators AND and OR in SQL
- Operator in C ++
- How to use Photoshop CS5 - Part 16: Delete any object from the image
- Array (Array) in PHP
- Steps to delete selections in Photoshop are very easy and effective
- ! = and! == What is the difference in JavaScript?
- Cursor and Array in C ++
Maybe you are interested
How does stress damage memory and cause anxiety disorders?
The world's 'stupid' bird, at the moment of life and death it 'loses its memory'
Nvidia launches RTX 4070 version with 'super fast' GDDR6 memory
Nvidia is developing a line of AI GPUs with 144GB HBM3E memory
3 Ways to free up Google Drive space, increase memory
Samsung begins mass production of 'most advanced' 12nm DDR5 memory chips