The free function in the C language determines how much memory space to release

Source: Internet
Author: User

This article link:http://www.cnblogs.com/xxNote/p/4009359.html

Reading today when you see the free function to release the memory of the dynamic application only need to pass the first address of the memory block, it is clear that only the first address is unable to determine how much memory to release, guess should be somewhere to store the size of this memory block, Search on the internet found in the Linux glibc in allocating memory when the memory block in front of the address of 4 bytes out of the size of the memory block, it is assumed that Windows should be the same. Wrote a small program tested under:

#include <stdio.h> #include <stdlib. H>int Main (void) {int *p = (int *) malloc (int); *p = 1;free (p); return 0;}

To run debugging, find the address of this memory block first:

Now look at the memory:

Since the application of 40 bytes of memory block, converted to 16 is 0x00000028, in memory is 28 00 00 00, look at the memory block before the first address of the 16 bytes and the results of the conjecture, then request a 400-byte size of memory block, Convert to 16 is 0x00000190, in memory is 90 01 00 00, and then look at the results of the operation:

is still the same as the inferred results, visible in Windows when we use the free function to release memory, the function will be the memory block of the first address plus 16, from this location can find the size of the memory block.

The free function in the C language determines how much memory space to release

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.