Learning C ++ pointers and dynamic memory allocation

1. Pointer

  1.1 pointer meanings:

  In simple terms, the pointer is a variable to store the memory address. When we declare a pointer variable, the system will use the address pointer of the memory block within the index pointer, the read value in the memory. Because the memory address pointer is used, it is generally a fixed length of 4 bytes. pointer to void pointer to a block of memory.

  Definition Example pointer:

int a=0;

int *Pointer_a=&a;

 

  When programming we can use the & (address references operator or operators) obtain the address of a variable or constant, for example, obtain a variable address is to use the example & a. For the memory address pointer for the pointer itself, plus * (dereferencing operator) reads the value at the address. The above embodiment, Pointer_a is a storage address, * Pointer_a is to read the value of a.

  Pointer arithmetic 1.2 (+ and -):

  And having a similar array pointer address arithmetic may be performed.

  We can use pointers to iterate try:

int the Array [ . 5 ] = { . 1 , 2 , . 3 , . 4 , . 5 };
 int * P = the Array; // the Array address stored in the first element of the array 
for ( int I = 0 ; I < . 5 ; I ++ ) 
{ 
    COUT << " the Array [ " << I + . 1 << " ]: " << * P << endl; 
    P ++ ; 
}

2. Dynamic Memory Allocation

  2.1 Using new and delete dynamically allocate and free memory:

  You can request a new allocated memory block (limited to the application may not be successful state of the system), if successful, returns a pointer, pointing to memory allocation, otherwise abnormal. delete to free up space for new allocated, when we use the new allocation of space is not in use, be sure to promptly release otherwise it will slow down the system.

  Dynamic memory allocation example:

  

int* p, * p_copy, number = 0;

 

"Please enter an integer number needs to be stored:" << COUT;
CIN >> Number;
p = new new int [Number]; // dynamic allocation of memory space needed
p_copy = p; p is the initial address of the storage //

 

cout << "Please enter each integer (separated by spaces):";
for (int I = 0; I <Number; I ++) // input integers
{
  CIN * >> p_copy;
  p_copy ++;
}
for (int I = 0; i <number; i ++ ) // integer value in each spatial output dynamic allocation
{
  COUT << "first" << i + 1 << "integer:" << endl << * P;
  P ++;
}

  

  2.2 Dynamic allocation of memory space considerations:

  After using the new release does not allocate memory space, resulting in slower system to run longer. Pay attention with the use of new and delete, otherwise it will lead to memory leaks. After we used to delete the original valid pointer pointer will become invalid pointer, this time for the suspension pointer pointer. There is no pointer after a defined point or memory space to store variable addresses, so the pointer is invalid. Invalid pointer dereference program tend to be abnormal. Therefore, we can initialize the pointer is NULL, the pointer is valid checked before use.

  2.3 Exception handling dynamic allocation of memory space:

  If the allocation is not successful in the use of new, it will make the program interruption, and pop-up error window. Which we can write an exception handler, normal execution if allocation is successful, unsuccessful can properly exit.

  E.g:

 

the try 
{ 
    int * P = new new  int [ 536870911 ];
     Delete [] P; 
} 
the catch (bad_alloc) 
{ 
    COUT << " memory allocation fails, program end " << endl; 
}

 

  Or using new (nothrow), the allocation fails, return NULL.

  E.g:

int * p = new new (the nothrow) int [ 0x1FFFFFFF ];
 IF (p) // Check whether the empty p 
{
     Delete [] p; // when p is empty, the release of p memory 
}
 the else 
    COUT << " memory allocation failure, the program exits " << endl;

 

 

  

Guess you like

Origin www.cnblogs.com/dulm/p/11240703.html