[C Resolution 8] dynamic memory management

Source: Internet
Author: User

C parsing-8 dynamic memory managementDynamic Memory Management: the charm of C/C ++ is that it can make youComputer Control has risen to an almost unlimited level.
Dynamic Memory Management and pointers allow C/C ++ to precisely control the memory. This feature highlights the space consumption of C/C ++ programs, at a certain level, we can explain why C is the first choice for the underlying system development language. From operating system tasks to an address book, you can find dynamic memory management. If you study it in depth, you will find that dynamic memory management plays an irreplaceable role. 1. Dynamic Memory Management and pointers: The main functions of dynamic memory management, malloc () and free (), all require pointer support. For more information about pointers, see [C parsing 4] pointers. The C language memory allocation mechanism ([C parsing 3] C language memory allocation) tells us that the dynamic space application is in the heap zone, the malloc function applies for space, and the free function is used for release. What role does a pointer assume? Global variables and static variables stored in the static area, and local variables in the stack area (about the local variable [C Resolution 5] function: do not know anything about it) and so on. In other words, these variables have names and types. You can use these variables by name. However, in the heap area, the requested space through dynamic allocation does not have a name or type, and using the space through pointers seems to be the only choice. At the same time, no type is specified for the applied space, and a forced conversion is required before being assigned to the pointer. Understanding these two points is not difficult to understand the function prototype, as shown below:
2. malloc () opens up a specified storage space: Usage: malloc (int size). size indicates the number of bytes to be opened.Return Value: the first address of the open space. 3. free ()Space to release memory space:
Usage: free (void * p), p is the pointer to the Open SpaceReturn Value: no return value. The following is an example of using malloc () and free:
4. Advantages of dynamic memory management:
The so-called dynamic memory management also means that it can dynamically meet the demand for memory space. In many cases, we do not know how much space is required to meet the requirements, such as recording the scores of class students. Some have a large number of students and some have a small number of students, if the defined array is too large, the memory is wasted. If it is too small, the problem may occur. In this case, the dynamic memory management method is used to open up the corresponding size space according to the number of students in different classes, it will not waste any memory of the program. The linked list may be the best definition of dynamic memory management. Through memory management, the linked list can store data from zero to infinite (if the memory is sufficient). This part will be detailed in the future. The following is an example of dynamically allocating memory records class scores and displaying average scores:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.