Principle of C language free function ---- [Badboy], free function badboy

Source: Internet
Author: User

Principle of C language free function ---- [Badboy], free function badboy
Today, I saw such a problem on the Internet: "If malloc has a string of memory, then it changes the size of the string and asks if some memory is not released ." I did not think about this issue carefully before.

Of course, I think it will definitely be released, but I have never understood the principle of free and dare not talk about it. I have looked at the memory management of the operating system. Basically, this is the case. Of course, the implementations of various systems are different.

The operating system manages the memory and maintains a idle memory linked list. malloc selects one from the linked list for use. Each memory block has a header to indicate the basic information of the memory, such as memory size,

Therefore, when you are free, you can remember the size of the memory that the original pointer refers to, instead of using \ 0 in the memory block to temporarily calculate the size pointing to the memory. Do not mislead the string length calculation method.

Note that the system only remembers the address of the malloc and the size of the allocated memory when there is free memory.

For example, char * p = (char *) malloc (10); 10 bytes are allocated. If you change the pointer address to p = p + 1, then free will cause problems. The program will crash.

If you must change the pointer value, we recommend that you do char * newp = p; then change newp = newp + 1, and finally free (p );

Note that a 10-character string occupies 11 bytes. Because there is another '\ 0', the size of lenght + 1 should be allocated during memory allocation.


The free function in C Language

To be free, the first part of the space should be malloc, calloc, realloc... the memory space opened up. The amount of free depends on the amount of alloc at that time.
Char * a = (char *) malloc (x * y * sizeof (char); // opens up the space of x * y char (here it is two-dimensional, if multi-dimensional data is used, it is used );
// Equivalent to a [x] [y];
// But the space produced by malloc is in the heap area, while the array is in the stack area. The two are continuous spaces. The stack space is limited, generally 1 k; more heap space;
... // Use this space;

Free (a); // release space;

Usage of free function in C Language

1. Basic Concepts and usage of malloc () and free:
1. function prototype and description:
Void * malloc (long NumBytes): This function allocates NumBytes bytes and returns a pointer to this memory. If the allocation fails, a NULL pointer is returned ).
There are many reasons for allocation failure, such as insufficient space.
Void free (void * FirstByte): This function returns the space previously allocated with malloc to the program or the operating system, that is, this memory is released, so that it is free again.
2. Function usage:
In fact, these two functions are not very difficult to use, that is, after malloc () is used up, I think it is enough to give it free (). For example:
// Code...
Char * Ptr = NULL;
Ptr = (char *) malloc (100 * sizeof (char ));
If (NULL = Ptr)
{
Exit (1 );
}
Gets (Ptr );
// Code...
Free (Ptr );
Ptr = NULL;
// Code...
That's it! Of course, the specific situation should be analyzed and solved. For example, if you have defined a pointer, applied for a piece of memory in a function, and passed it to the pointer through the function return, then the release of the memory should be left to other functions.
3. Notes for using functions:
A. After applying for memory space, you must check whether the allocation is successful.
B. When you do not need to use the requested memory, remember to release it. After the memory is released, point the pointer to this memory to NULL to prevent the program from using it accidentally.
C. These two functions should be paired. If the application is not released, the memory is leaked. If the application is released without reason, nothing is done. Only one release is allowed. If two or more releases are performed
An error occurs (with the exception of releasing the NULL pointer, releasing the NULL pointer does not actually do anything, so it is no problem to release the NULL pointer many times ).
D. Although the type of the malloc () function is (void *), any type of pointer can be converted to (void *), but it is best to force type conversion before, because this can escape
Some compiler checks.

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.