Latest web development tutorials

C ++ dynamic memory

Understand the dynamic memory in C ++ it is how the work is to become a qualified C ++ programmers essential. C ++ program memory is divided into two parts:

  • Stack: all variables declared inside a function will take up the stack memory.
  • Heap: This is theprogram memory is not used, the program runs can be used to dynamically allocate memory.

Many times, you can not predict in advance how much memory is required to store a variable defined in the specific information required to determine the size of memory needed at runtime.

In C ++, you can use a special operator is assigned to a variable of a given type of memory within the heap at run time, it returns the address space allocated. This operator is beingnew operator.

If you do not need to dynamically allocate memory, you can use thedelete operator, deleted by the new operator before the allocated memory.

new and delete operators

Here is the new operator to be any general syntax of data type dynamic allocation of memory:

new data-type;

Here, data-type can be any built-in data types, including arrays, can be any type of data including a class or structure, including user-defined.Let us first look at the built-in data types. For example, we can define a pointer to a pointer of type double, then requests memory, the memory is allocated at run time. We can use thenew operator in accordance with the following statement to accomplish this:

double* pvalue  = NULL; // 初始化为 null 的指针
pvalue  = new double;   // 为变量请求内存

If the free storage areas have been exhausted, it may not successfully allocate memory. It is advisable to check whether the new operator returns a NULL pointer, and take the appropriate action the following:

double* pvalue  = NULL;
if( !(pvalue  = new double ))
{
   cout << "Error: out of memory." <<endl;
   exit(1);

}

malloc () function in the C language appeared in C ++ is still present, but it is recommended not to use malloc () function.new and malloc () function compared to its main advantage is that, not only allocate new memory, it also created the object.

At any time, when you feel that a dynamic variables already allocated memory is no longer needed, you can use the delete operator to release the memory it used, as follows:

delete pvalue;        // 释放 pvalue 所指向的内存

Used in the above concept, the following example demonstrates how to use the new and delete operators:

#include <iostream>
using namespace std;

int main ()
{
   double* pvalue  = NULL; // 初始化为 null 的指针
   pvalue  = new double;   // 为变量请求内存
 
   *pvalue = 29494.99;     // 在分配的地址存储值
   cout << "Value of pvalue : " << *pvalue << endl;

   delete pvalue;         // 释放内存

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Value of pvalue : 29495

An array of dynamic memory allocation

Suppose we want a character array (a string of 20 characters) to allocate memory, we can use the above example of array syntax to dynamically allocate memory, as follows:

char* pvalue  = NULL;   // 初始化为 null 的指针
pvalue  = new char[20]; // 为变量请求内存

To delete an array we just created, the statement is as follows:

delete [] pvalue;        // 删除 pvalue 所指向的数组

Here is the general syntax new operator can allocate memory for the multidimensional arrays, as follows:

int ROW = 2;
int COL = 3;
double **pvalue  = new double* [ROW]; // 为行分配内存

// 为列分配内存
for(int i = 0; i < COL; i++) {
    pvalue[i] = new double[COL];
}

Release multidimensional array memory:

for(int i = 0; i < COL; i++) {
    delete[] pvalue[i];
}
delete [] pvalue; 

Dynamic memory allocation object

The object is not different from simple data types. For example, consider the following code, we will use an array of objects to clarify this concept:

#include <iostream>
using namespace std;

class Box
{
   public:
      Box() { 
         cout << "调用构造函数!" <<endl; 
      }
      ~Box() { 
         cout << "调用析构函数!" <<endl; 
      }
};

int main( )
{
   Box* myBoxArray = new Box[4];

   delete [] myBoxArray; // Delete array

   return 0;
}

If you want a Box contains four objects allocate memory array, the constructor will be called four times, in the same manner, when you delete these objects, the destructor will be called the same number of times (four times).

When the above code is compiled and executed, it produces the following results:

调用构造函数!
调用构造函数!
调用构造函数!
调用构造函数!
调用析构函数!
调用析构函数!
调用析构函数!
调用析构函数!