Why and when do we need to dynamically allocate memory in C language?

 

Memory management is an operation close to the physical nature of the computer, and the actions under the programming language must eventually be realized by mobilizing the memory. System resources are not unlimited, and the programs running on the system are not the only one. If memory is ignored, dangerous and redundant code products will be designed, or there will be no better interaction.

Why do we need dynamic memory allocation?

Memory is not inexhaustible. 4g, 8g, and 16g are common computer memory sizes. Open the task manager and you can see the memory occupied by different applications. If an application occupies most of the memory, it is estimated that other applications are running out of resources, then this application may be uninstalled and find a memory-saving one.

Streamlined applications can use memory more effectively, instead of burying their heads in business logic, and finally bring out very resource-intensive applications.

When the use of resources is small and the amount of code is small, memory leaks are rarely involved, and memory management is not involved, especially the outdated textbooks of current C language teaching, old exercises, and memory management It is almost untouched, and those who have learned it will not realize the use of memory management.

I saw an explanation at https://stackoverflow.com/questions/24891/c-memory-management . It’s very well written. I put it here, and it’s in plain English. I won’t translate it. You can find the original author in the link.


Question 

I've always heard that in C you have to really watch how you manage memory. And I'm still beginning to learn C, but thus far, I have not had to do any memory managing related activities at all.. I always imagined having to release variables and do all sorts of ugly things. But this doesn't seem to be the case.

Can someone show me (with code examples) an example of when you would have to do some "memory management" ?

Answer

There are two places where variables can be put in memory. When you create a variable like this:

int  a;
char c;
char d[16];

The variables are created in the "stack". Stack variables are automatically freed when they go out of scope (that is, when the code can't reach them anymore). You might hear them called "automatic" variables, but that has fallen out of fashion.

Many beginner examples will use only stack variables.

The stack is nice because it's automatic, but it also has two drawbacks: (1) The compiler needs to know in advance how big the variables are, and (b) the stack space is somewhat limited. For example: in Windows, under default settings for the Microsoft linker, the stack is set to 1 MB, and not all of it is available for your variables.

If you don't know at compile time how big your array is, or if you need a big array or struct, you need "plan B".

Plan B is called the "heap". You can usually create variables as big as the Operating System will let you, but you have to do it yourself. Earlier postings showed you one way you can do it, although there are other ways:

int size;
// ...
// Set size to some value, based on information available at run-time. Then:
// ...
char *p = (char *)malloc(size);

(Note that variables in the heap are not manipulated directly, but via pointers)

Once you create a heap variable, the problem is that the compiler can't tell when you're done with it, so you lose the automatic releasing. That's where the "manual releasing" you were referring to comes in. Your code is now responsible to decide when the variable is not needed anymore, and release it so the memory can be taken for other purposes. For the case above, with:

free(p);

What makes this second option "nasty business" is that it's not always easy to know when the variable is not needed anymore. Forgetting to release a variable when you don't need it will cause your program to consume more memory that it needs to. This situation is called a "leak". The "leaked" memory cannot be used for anything until your program ends and the OS recovers all of its resources. Even nastier problems are possible if you release a heap variable by mistake before you are actually done with it.

In C and C++, you are responsible to clean up your heap variables like shown above. However, there are languages and environments such as Java and .NET languages like C# that use a different approach, where the heap gets cleaned up on its own. This second method, called "garbage collection", is much easier on the developer but you pay a penalty in overhead and performance. It's a balance.

 

Guess you like

Origin blog.csdn.net/sinat_39416814/article/details/104797279