About request for malloc memory and free memory release

Source: Internet
Author: User

malloc working mechanism:

mallocThe physical body of the function now, it has a so-called idle list that connects the available memory blocks to a long table(Heap Memory)。 Callmallocfunction, it looks for a block of memory along the join table that is large enough to satisfy the user's request. The memory block is then split in two (the size of the chunk is equal to the size of the user request, and the other is the remaining bytes). Next, pass the memory allocated to the user to the user and return the rest of the block (if any) to the connection table. Call Freefunction, it connects the memory blocks freed by the user to the idle chain. In the end, the idle chain will be cut into a lot of small memory fragments, if the user requests a large memory fragment, then the idle chain may not be able to meet the user requirements of the fragment. SomallocThe function requests the delay, and begins to rummage through the memory fragments on the idle chain, organizes them, and merges the adjacent small free blocks into larger chunks of memory. If you cannot obtain a block of memory that meets the requirements,mallocfunction will returnNULLPointer, so the callmallocWhen requesting a memory block dynamically, be sure to make a judgment return value.


The program occupies three kinds of memory: static memory, stack memory, heap memory;

Static memory: Used to hold local static objects, class static data members, and variables defined outside of any function

Stack memory: Used to hold non- static objects defined within a function. Objects allocated in static memory or in stack memory are automatically created and destroyed by the compiler. For a stack object, it exists only when its defined block is run, and thestatic object is allocated before it is used and destroyed at the end of the program.

Heap Memory: Allocated when the program is running. The life cycle of a dynamic object is controlled by the program (user).

Questions about whether the actual data exists after releasing memory using the free () function

With regard to the memory allocated with malloc , The state of the memory after the call to free is made with the following notes:

1. call free to release the allocated memory, indicating that the memory can be used by others, that is, the other place after malloc , can be allocated to the memory

2. regarding the free release of the memory, we can only think of the data in this memory as dirty, that is to say, this part of the data may exist and maintain the original value, it may be emptied, or modified to other values;

3. After releasing the memory, in addition to assigning a value of NULLto the assigned pointer at that time, be careful not to refer to this part of memory, and not try to get the value of this part, which is illegal.

4. After the memory is free, the data inside is still in, but this memory will be marked as usable memory, other programs can use the memory inside the data may exist, as long as the memory is not occupied elsewhere. Your free release is the use of memory.

About request for malloc memory and free memory release

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.