C 内存管理 [英] C Memory Management

查看:23
本文介绍了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.

有人可以向我展示(带有代码示例)何时必须进行一些内存管理"的示例?

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

变量在堆栈"中创建.堆栈变量在超出范围时会自动释放(即,当代码无法再访问它们时).您可能会听到它们被称为自动"变量,但这已经过时了.

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)编译器需要提前知道变量有多大,(2)堆栈空间有些有限.例如:在 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 (2) 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)

一旦你创建了一个堆变量,问题是编译器不能告诉你什么时候完成,所以你失去了自动释放.这就是手动释放"的地方.你指的是进来.你的代码现在负责决定何时不再需要变量,并释放它以便内存可以用于其他目的.对于上述情况,使用:

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.

在 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天全站免登陆