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.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章