C内存管理 [英] C Memory Management

查看:140
本文介绍了C内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直听说,在C你要真看你是如何管理内存。我仍然开始学习C,但迄今为止,我还没有做任何的内存管理相关活动所有..我总是幻想着有释放变量,并做各种丑恶的东西。但是,这似乎并不如此。

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.

有人能告诉我(与code的例子)时,你将不得不做一些内存管理的例子吗?

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

推荐答案

有两个地方变量可以把内存中。当你创建一个变量:

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];

该变量在创建了。当他们走出去的范围堆栈变量自动释放(即当code不能达到他们了)。你可能会听到他们所谓的自动变量,但已经跟不上潮流。

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.

堆栈是很好的,因为它是自动的,但它也有两个缺点:(1)编译器需要预先知道的变数有多大,以及(b)的堆栈空间是比较有限的。例如:在Windows下的Microsoft连接器的默认设置,堆栈被设置为1 MB,而不是所有的它是可用于你的变量

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.

如果你不知道在编译时你的数组有多大,或者如果你需要一个大数组或结构时,需要B计划。

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".

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)

一旦你创建一个堆变量,问题是,当你用它做编译器不能告诉,让你失去了自动释放。这就是手动释放你指的进来。你code是现在负责决定何时不再被需要的变量,并释放它,因此存储器可以采取用于其他目的。对于以上的情况下,与

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);

是什么让这个第二个选项肮脏生意的是,它并不总是很容易知道什么时候已经不再需要的变量。忘记释放一个变量,当你不需要它会导致你的程序占用更多的内存,它需要。这种情况被称为泄漏。该泄露内存不能被用于任何东西,直到您的程序结束和OS恢复其所有资源。如果你释放错误的堆变量之前的你实际上是用它做即使令人讨厌的问题是可能的。

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.

在C和C ++,您有责任清理堆变量,如上面所示。不过,也有语言和环境,如Java和.NET语言如C#使用了不同的方法,其中堆得到对自己清理。第二种方法,被称为垃圾收集,是对开发者更容易,但你的开销和性能支付违约金。这是一个平衡点。

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.

(我已经掩盖了很多细节要给出一个简单的,但希望更平整的答案)

这篇关于C内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆