One type of C ++ new / delete Detailed and principles

      Learn the Von Neumann architecture, we know: hardware software decision behavior, the data is flowing around the memory.

      One can imagine how memory is important. Of course, we are here to say that memory is the virtual memory (see details of Linxu One type).

    1.C / C ++ memory layout  

     2.C language dynamic memory management

       Application Memory: malloc / calloc / realloc
       release: as Free
       malloc / calloc / realloc the difference:
          

// Application of the size of the memory block size 
void * the malloc (size_t size); 

// application size num * size of the memory block, and each element is initialized to 0 
void * calloc (size_t NUM, size_t size); 

// ptr is NULL, the application space is similar to malloc
 // ptr is not NULL, then ptr point to change the size of the space size, and if the change in space is much larger than the old space, will apply for a new block of memory and copy the data over the original release old space, returns the new address 
void * realloc ( void * ptr, size_t size);

 

      3.C ++ dynamic memory management

                   

      In C ++, the dynamic memory is no longer a function of the application, but the operator.

         Note that, the release of the individual elements of the application with new space and delete, applying continuous space with new release of [] and delete [].

      Then the new / delete and malloc / free in the end what difference does it Here, we do a test?:

#include <the iostream> 
#include <malloc.h> 
the using namespace STD; 

class the Test 
{ 
public: 
	the Test () 
		: the _data (0) 
	{ 
		COUT << "the Test ():" << endl << the this; 
	} 
	~ the Test ( ) 
	{ 
		COUT << "~ Test ():" << endl << the this; 
	} 
Private: 
	int the _data; 
}; 

void Test2 () 
{ 
	// Test application single type of spatial 
	Test * p1 = (Test *) malloc ( the sizeof (Test)); 
	Free (P1); 
	// Test application 10 types of spatial 
	Test P2 * = (Test *) the malloc (the sizeof (Test) * 10); 
	Free (P2); 
} 

void the Test3 () 
{ 
	/ / application of a single type of objects Test  
	Test * p1 = new Test;
	delete p1; 
	// Application object type 10 Test 
	Test Test new new * P2 = [10]; 
	Delete [] P2; 
} 

int main () 
{ 
	Test2 (); 
	the Test3 (); 
	getchar (); 
	return 0; 
}

       Test results are as follows:

                  

                   We can get the result: new new space will first apply for and then call the constructor, delete the destructor will be called first, and then release the space. And malloc / free calls not configured / destructor.

      

    4.new/delete principle

      We know the usage and characteristics of new / delete, the next they begin to understand the underlying principles.

      are new and delete users dynamic memory and freeing operators, operator new and operator delete global functions provided by the system.

      new call operator new function for the space at the bottom, delete to free up space by operator delete function at the bottom.

      The following is an operator new pseudo-code:

      

       The following is the operator delete pseudo-code:

         

 

 

       Thus, we know that operator new and operator delete the underlying calls to malloc and free, so call the following relationship:

                   

      to sum up:

        

 

      

Guess you like

Origin www.cnblogs.com/Duikerdd/p/11687621.html