The heap heap memory management

1. What is the heap?
 Heap (heap) is a type of memory management. Memory management of the operating system is a very complicated matter, because first of all the memory capacity is large,
 the second is not the law of memory requirements in terms of time and the size of the block (running tens or even hundreds of processes on the operating system, these processes perhaps
 you would apply or release the memory, and the application and release of the memory block size is optional).
 This feature is free heap memory management method (apply at any time, at any time release, the block size random). Heap memory to the operating system under the stack pipe
 manager (section of code in the operating system, part of the operating system's memory management unit) to manage, and provide to the user (user process)
 the API (the malloc and free) to the heap RAM.  When we use the heap memory?  When we need a relatively large memory capacity, the need for repeated use and release, to achieve a lot of data structure (such as a linked list) must use the heap memory. 2, the memory heap management features (large memory allocation is performed manually using & & release)  features a: limited capacity (capacity requirements to meet are conventionally used)  Features 2: Application and release requires manual operation carried out manually meaning is the need for programmers to write code that explicitly apply malloc and release free.  If the application programmer does not release memory and use, this memory is lost (in the heap manager's record, this memory is still yours to this process,  but the process himself has not thought this memory, so the process again when running, it will go to apply for a new block of memory, which is called eating memory),     known as a memory leak. In the C / C ++ language, memory leak is the most serious procedural bug, others think this is Java / C # and other languages than good C / C ++ place. 3, C language interface operation of heap memory (malloc free)
 


 







    When heap release of the easiest, free release can be called directly. void free (void * ptr);
 heap memory allocation, a function of similar function has three selectable: the malloc, calloc, realloc
 void * the malloc (size_t size);
 void * calloc (size_t of nmemb, size_t size); // nmemb units, each unit size bytes
 void * realloc (void * ptr, size_t size); // change the original size space applications  such as application to the memory elements 10 int:  the malloc (40); the malloc (10 the sizeof * (int));  calloc (10,. 4); calloc (10, the sizeof (int));  must be given number of array elements (array size) while an array definition and can not be changed once defined.  Heap memory must be given when applying for a given size, and then once the application is completed the same size, if you want to change only through realloc interface. 4, heap advantages and disadvantages (large chunks of memory management, flexible, easy to memory leaks)  Advantage: Flexible  Disadvantages: requires the programmer to deal with all the details, so error-prone, heavily dependent on the level of the programmer.
 



 




Guess you like

Origin www.cnblogs.com/jiangtongxue/p/11352586.html