内存、原语和 c

发布于 2024-11-03 22:01:29 字数 297 浏览 1 评论 0原文

假设我正在声明原语或原语数组(在堆栈右侧)。我如何“释放”或清理它们,这样它们就不会占用/泄漏内存?

例如:

int blah;
blah=7;
doSomethingWithBlah...
free(blah) //[?]

通常情况下可能没有一点交易;但是如果你说一个巨大的字符数组或其他什么怎么办?

此外,任何“局部”变量(即函数内部)默认情况下都将是“auto”,并且会自动清除[用 Java 术语来说,它们在超出范围时会被 GC 处理)。

只是好奇,谢谢。

Say I'm declaring primitives or arrays of primitives (on the stack right). How do I "free" or clean them up so they aren't taking/leaking memory?

For example:

int blah;
blah=7;
doSomethingWithBlah...
free(blah) //[?]

probably not a bit deal normally; but what if you had say a huge char array or whatever?

also any 'local' variables (that is, inside a function) will be by default 'auto' and are cleaned up automatically right [in Java terms, they are GCed when going out of scope).

Just curious, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦与时光遇 2024-11-10 22:01:29

你不知道。

堆栈是一种数据结构。它是 LIFO(后进先出),这意味着最后放入堆栈的内容是第一个从堆栈中删除的内容。以下是在编程中如何思考这一点:

main -> foo

主函数(入口点)调用某个函数 foo。某些变量在主函数中声明/定义。其他的在 foo 函数中声明,它们处于不同的作用域级别。

当您进入程序时,main 中声明的所有变量都被推入堆栈的一个块中。与 foo 被调用时相同,但是是下一个块。然后,当它到达 foo 函数的末尾时,它会将这组变量从堆栈中弹出(或“释放”它们)。当它到达主函数的末尾时,它将该组变量从堆栈中弹出。

因此,为了回答您的问题,所有局部变量(包括存储在堆栈中的局部数组)在超出范围时都会自动删除。您永远不必自己释放它们。

您只需担心放入堆中的内容,即在 C 中使用“malloc”或在 C++ 中使用“new”时的情况。

You don't.

A stack is a data structure. It's LIFO (Last In, First Out) which means that the last thing that was put on the stack is the first one to be removed from the stack. Here's how you can think about this in programming:

main -> foo

The main function (entry point) calls some function foo. Certain variables are declared/defined in the main function. Others are declared in the foo function, which are at a different level of scope.

When you enter the program, all the vars declared in main are pushed onto a block of the stack. Same with foo when it is called, but the next block. Then when it reaches the end of the foo function, it pops that set of variables off the stack (or 'frees' them). When it reaches the end of the main function, it pops that set of variables off the stack.

So to answer your question, ALL local variables, including local arrays which are stored on the stack, are automatically deleted when they go out of scope. You never have to free those yourself.

You just worry about the stuff that you put on the heap, which is anytime you use "malloc" in C, or "new" in C++.

π浅易 2024-11-10 22:01:29

当堆栈帧被销毁时(即当函数返回时),堆栈上的局部变量将不再存在。当它们的堆栈框架仍然存在时,无法删除它们。

如果您需要一个大数组,则应使用 malloc() 在堆上分配它,以便在使用完毕后可以free() 它。 (用于访问数组的指针变量保留在堆栈上,但它很小。)

Local variables on the stack cease to exist when the stack frame is destroyed (i.e. when the function returns). There's no way to remove them while their stack frame still exists.

If you need a large array, you should allocate it on the heap using malloc(), so that you can free() it when you're done with it. (The pointer variable used to access the array remains on the stack, but that's small.)

听你说爱我 2024-11-10 22:01:29

您不需要从堆栈中“清理”或“释放”数据,没有必要,因为一旦函数返回,内存就可以再次使用。所以 blah 不应该被“清理”(但是一旦函数返回你就不能传递对它的引用......)

You don't "clean" or "free" data from the stack, there is no need since the memory is available for use again once the function returned. So blah should not be "cleaned" (but you can't pass a reference to it once the function return either...)

吹泡泡o 2024-11-10 22:01:29

据我所知,在 C 中在堆栈上声明的局部变量将被分配为函数激活记录的一部分,因此在函数返回时被回收。

我不相信有任何方法可以“释放”堆栈上声明的数据。

To the best of my knowledge, local variables declared on the stack in C will be allocated as part of a function's activation record, and therefore reclaimed when the function returns.

I don't believe there's any way to "free" data declared on the stack.

故事还在继续 2024-11-10 22:01:29

你不能。当堆栈变量超出范围(在包含块的末尾)时,堆栈变量将被“释放”。

大型结构/数组/等您在堆上进行malloc。

请注意,您说“用 Java 术语来说,它们在超出范围时会被 GC” ...这是不正确的,也是一种糟糕的思考方式。 java 中的所有对象都是在堆上创建的,当没有对它们的剩余引用时,它们可供垃圾收集器使用。这与 C 非常非常不同,在 C 中,您可以在堆栈上创建内容并(错误地)将指向它们的指针返回给调用者。

You can't. Stack variables are "freed" when they go out of scope (at the end of the containing block).

Large structures / arrays /etc you malloc on the heap.

Note that you say "in Java terms, they are GCed when going out of scope" ... this is incorrect and a bad way to think about things. All objects in java are created on the heap, and are made available to the garbage collector when there are no remaining references to them. This is very, very different than C where you can create things on the stack and (incorrectly) return pointers to them to a caller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文