Process Environment Details (v)---heap allocation malloc, free functions detailed

Source: Internet
Author: User

The space on the heap is not contiguous, and the space on the stack is continuous.     You can use the malloc function to get the memory space on the heap, noting that although the space on the heap is discontinuous, the call to the malloc function allocates or continues memory. For example, malloc (10) finds a contiguous space of 10 bytes on the heap to be allocated to the user program, and then the malloc (5) will find a contiguous space of 5 bytes to the user program on the heap, and the two allocated space is not contiguous.  allocating memory space using malloc requires the user to call the free () function manually to release it, which causes a memory leak. However, you cannot use the free () function to release the same memory space multiple times because the memory is not part of the user program after the first release, and releasing again may cause memory errors. So in order to prevent this, after freeing space with the free () function, place the pointer null immediately, so that even if the free () function is released again, because the parameter is NULL, you can tell from the man manual that there is nothing to do with the function. ================================================================
    • You can use the ReAlloc function to change the size of the memory on the allocated heap. The ReAlloc function returns a new pointer to the new memory address if the call succeeds, and frees the previously allocated memory space, so be aware that the original memory space cannot be freed again, and if the call fails, it returns NULL, and the original memory area does not change. Therefore, ptr = ReAlloc (ptr, size) is not normally used, and if the return fails, PTR becomes null, and the original memory area is no longer found.
 void *realloc (void *ptr, size_t size), where size is the amount of space after the change. realloc (NULL, size) < = = > malloc (size);realloc (PTR, 0) < = = > free (PTR); 
    • void *calloc (size_t nmemb, size_t size);
The calloc function, like the malloc function, allocates nmemb*size-sized memory space on the heap. What is added to malloc is that the Calloc function will completely zero the allocated memory content.
    • The Alloca function differs from the MALLOC series function by using it to request that the memory space is on the stack and that the slice of memory is automatically freed when the function returns.

Process Environment Details (v)---heap allocation malloc, free functions detailed

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.