是(字符)数组的内存走出去的范围中解放出来? [英] Is the memory of a (character) array freed by going out of scope?
问题描述
有很大关系<一个href=\"http://stackoverflow.com/questions/1322884/c-does-going-out-of-scope-like-this-free-the-associated-memory\">my previous问题,但我发现这是一个单独的问题,我无法找到一个坚实的答案。
Very much related to my previous question, but I found this to be a separate issue and am unable to find a solid answer to this.
是由一个(角色)所使用的存储阵列通过走出去的范围中解脱出来?
Is the memory used by a (character) array freed by going out of scope?
一个例子:
void method1()
{
char str[10];
// manipulate str
}
所以方法1调用后,被释放由乙方(10字节)使用的内存,或者我需要显式调用免费这个呢?
So after the method1 call, is the memory used by str (10 bytes) freed, or do I need to explicitly call free on this as well?
我的直觉告诉我,这是原始类型的只是一个简单的数组,所以它的自动释放。我很怀疑因为在C你不能假设任何事情被自动释放。
My intuition tells me this is just a simple array of primitive types, so it's automatically freed. I'm in doubt because in C you can't assume anything to be automatically freed.
推荐答案
在此情况下,没有你不需要打电话免费。值海峡,是一个基于堆栈的值,当特定的方法/范围退出将被清理。
In this case no you do not need to call free. The value "str" is a stack based value which will be cleaned up when that particular method / scope is exited.
您只需要拨打免费的,它们通过明确的malloc创造的价值。
You only need to call free on values which are explicitly created via malloc.
这篇关于是(字符)数组的内存走出去的范围中解放出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!