C语言中为什么、什么时候要动态分配内存?

内存管理是计算机接近物理本质的操作,那些程序语言之下的动作,最终都要调动内存来实现。系统的资源不是无限的,系统上运行的程序也不是只有这一个,忽略内存,就会设计出危险的、冗余的代码产品,或者没法更好的交互。

为什么要进行动态内存分配?

内存不是取之不尽用之不竭,4g、8g、16g是常见的电脑内存大小,打开任务管理器,能看到不同的应用占据的内存情况。如果一个应用程序占了大部分内存,估计别的应用就资源紧张了,那这个应用可能会被卸载,找个节省内存的。

精简的应用能更有效地使用内存,而不是埋头搞业务逻辑,最后却整出来非常耗费资源的应用来。

在资源使用很小,代码量很小的时候,很少会涉及到内存泄漏的问题,也就不涉及内存管理的事情,尤其是当前C语言教学陈旧的教材,里面陈旧的习题,和内存管理几乎不沾边,学过的人不会意识到内存管理有什么用。

https://stackoverflow.com/questions/24891/c-memory-management看到了一个解释写的很不错,放这里,用的都是简单的英语就不翻译了,可以在链接中找到原作者


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.

 

猜你喜欢

转载自blog.csdn.net/sinat_39416814/article/details/104797279