8, allocate memory on the heap

xiaoxiao2021-03-06  77

---------

Many people may not understand "Stack Stack" and "Heap" on memory allocation. People who include some barborses are not understanding these two concepts. I don't want to say more about these two things. Simply put, the memory system allocated on the Stack is automatically released, the memory allocated on the HEAP is not released, even if the program exits, the memory is still there. Stack is typically static allocation of memory, and HEAP is generally dynamically allocated.

The memory allocated by the Malloc system is allocated from the heap. The memory allocated from the pile must be released by himself. Use free release, or the term - "Memory Leak" (or "Memory Vulnerability") - Memory Leak. Thus, the system's allocated memory will be less and less Mallo, until the system crashes. Let's take a look at the difference between "stack memory" and "stack memory".

Stack memory allocation

-----

Char *

Allocstrfromstack ()

{

Char PSTR [100];

Return PSTR;

}

Stack memory allocation

-----

Char *

AllocstrFromHeap (int LEN)

{

CHAR * PSTR;

IF (len <= 0) Return NULL;

Return (Char *) Malloc (LEN);

}

For the first function, the internal presence of the PSTR is released by the system when the function returns. So the return of the char * is nothing. For the second function, it is allocated from the heap, so even if the program is exited, it is not released, so the memory returned by the second function is no problem, which can be used. But must call free release, otherwise Memory Leak!

The distribution of memory is easy to cause memory leaks, which is the largest "gith" of C / C . If your program is stable, then Do not appear Memory Leak. So, I still have to pay a thousands of people here, be careful when using the Malloc system function (including Calloc, Realloc).

Remember to have a service application on UNIX, there are a few hundred C files compiled, run a good test, etc., the system is Down every three months, and many people can't find the problem. . I have to manually restart the system every two months. This problem is that MeMery Leak is strange, and this problem in C / C will always happen, so you must be careful. A Rational's Detection Work - Purify, you can help you test your programs do not have memory leaks.

I promise that the programmer of many C / C projects will have some cold in Malloc or New. When you use Malloc and New, there is a light tight and fearful feeling, you have this kind of cultivation.

For Malloc and Free operations have the following rules:

1) Pairing, there is a malloc, there should be a free. (C corresponds to new and delete)

2) Try to be used on the same layer, don't be like the above, Malloc in the function, and free outside the function. It is best to use these two functions on the same call layer.

3) Malloc allocated memory must initialize. The pointer behind free must be set to NULL.

转载请注明原文地址:https://www.9cbs.com/read-107743.html

New Post(0)