Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What are the specific uses of malloc and free?
What are the specific uses of malloc and free?
First, the basic concepts and usage of malloc () and free ():

1, function prototype and description:

Void *malloc(long NumBytes): This function allocates NumBytes and returns a pointer to this memory. If the allocation fails, a null pointer is returned.

There should be many reasons for the failure of distribution, such as insufficient space.

Void free(void *FirstByte): This function returns the space previously allocated by malloc to the program or operating system, that is, it releases this memory and makes it free again.

2, the usage of the function:

In fact, these two functions are not very difficult to use, that is, malloc () dumps them to free () after it thinks it is enough. Give a simple example:

Program code:

//code ...

char * Ptr = NULL

ptr =(char *)malloc( 100 * sizeof(char));

if (NULL == Ptr)

{

Exit (1);

}

gets(Ptr);

//code ...

Free of charge (PTR);

Ptr = NULL

//code ...

That's it! Of course, the specific situation should be analyzed and solved. For example, if you define a pointer, apply for a memory in a function and pass it to the pointer through the function return, then perhaps the work of releasing this memory should be left to other functions.

3, about the use of functions need to pay attention to some places:

A after applying for memory space, you must check whether the allocation is successful.

B, remember to release the memory of the application when it is no longer needed; After releasing, you should point the pointer to this memory to NULL to prevent the program from accidentally using it in the future.

C, these two functions should be paired. If it is not released after application, it is a memory leak; If you release it for no reason, you have done nothing. It can only be played once, and it will be played more than twice.

An error occurred (except for releasing the null pointer, which is actually equivalent to doing nothing, so it doesn't matter how many times you release the null pointer).

D, although malloc () function type is (void *), but any type of pointer can be converted to (void *), but it is best to force the type conversion in front, because this can avoid a.

Some compiler checks.

All right! That's probably what the most basic thing says! Now enter the second part:

Second, where did malloc () get the memory space?

Where did 1 and malloc () get the memory space? The answer is to get space from the heap. In other words, the pointer returned by the function points to a piece of memory in the heap. There is a linked list of free memory addresses in the operating system. When the operating system receives the application of the program, it will traverse the linked list, then find the first heap node whose space is larger than the application space, then delete the node from the idle node linked list and allocate the space of the node to the program. That's it!

Speaking of which, I want to add a small topic, I believe everyone knows what it is. What is a heap? Speaking of heaps, I can't help saying heaps! What is a stack? Let's talk about this digression in a small part:

2. What is a heap? Heap is everyone's space, which is divided into global heap and local heap. The global heap is all unallocated space, and the local heap is user-allocated space. Heaps are allocated during the initialization process of the operating system. You can also ask the system for extra heaps at run time, but remember to return them to the operating system when they are used up, otherwise it will be a memory leak.

What is a stack? Stack is unique to threads, and it saves the running state of threads and local automatic variables. The stack is initialized at the beginning of the thread, and the stack of each thread is independent of each other. Each function has its own stack for passing parameters between functions. The operating system will automatically switch the stack when switching threads, that is, switch the SS/ESP register. In high-level languages, there is no need to explicitly allocate and release stack space.

The above concept description is a standard description, but some sentences have been deleted by me, and I don't know why they have become nonstandard.

From the above description of the concept, we can know that:

The stack is automatically allocated and released by the compiler, and is used to store the parameter values of functions and the values of local variables. Operations are similar to stacks in data structures.

Heaps are generally allocated and released by programmers. If it is not released, it may be recycled by the OS at the end of the program. Note that it is possible here, but not necessarily. So I want to emphasize again, remember to release!