函数返回后,在函数中分配的内存是否仍保持分配状态? [英] Does memory allocated in a function still stay allocated after the function returns?

查看:41
本文介绍了函数返回后,在函数中分配的内存是否仍保持分配状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码:(1)"main"调用函数"f1".(2)函数"f1"进行一些数字运算;使用malloc创建一个"char"数组,然后将该数组的指针返回到主数组(不取消分配-freeing-该数组).

For the code below: (1) "main" calls a function "f1". (2) function "f1" does some number crunching; creates an array of "char" with malloc and then, returns the pointer of the array to the main (without de-allocating -freeing- the array).

我有3个与案件有关的问题:(1)我假设,尽管函数"f1"已终止,但分配的char数组仍保持分配状态,直到主程序完全终止为止.也就是说,分配的内存仍然属于主内存,其他任何进程都无法从外部访问(即干预)它.我对吗?(2)我是否必须在程序终止之前释放数组(在"f1"中分配)(或者在主程序终止时立即释放它)?(3)如果第二个问题的答案为是",那么如何释放在另一个函数中分配的数组?

I have 3 questions related to the case: (1) I assume, although the function "f1" has terminated, the allocated char array still stays allocated until the main program terminates completely. That is, the allocated memory still belongs to the main and no other process can access (I mean, interfere with) it from outside. Am I right? (2) Do I have to free the array (allocated in "f1") before the program terminates (or does it get freed as soon as the main program terminates) ? (3) If the answer for the second question is "yes" then how do you free an array allocated in another function?

注意:我想留在纯c的范围内,而不是溢出到c ++.

note: I want to stay within the boundaries of pure c and not to spill over to c++.

char *f1 (...) {
    ...
    ...
    char *fTmp = malloc (length1 * sizeof (char));
    char *fData = malloc (length2 * sizeof (char));
    ...
    ...
    free (fTmp);
    return (fData);
}

int main () {
    char *fData = f1 (...);
    ...
    return (0);
}

推荐答案

我认为,尽管函数"f1"已经终止,但是分配的char数组仍然保持分配状态,直到主程序完全终止.

I assume, although the function "f1" has terminated, the allocated char array still stays allocated until the main program terminates completely.

是的.动态分配的内存与功能无关,它属于进程.

True. Dynamically allocated memory has nothing to do with functions, it belongs to process.

也就是说,分配的内存仍然属于主内存,并且没有其他进程可以从外部访问它.我说的对吗?

That is, the allocated memory still belongs to the main and no other process can access it from outside. Am I right?

内存不属于 main()(旨在作为函数)而是属于自身(其中 main()只是入口).在具有内存保护的系统中(每个进程都与其他进程隔离),无法从外部进行访问.但是,您可以按系统特定的方式分配它,以在各个进程之间共享内存.

Memory doesn't belong to main() (intended as function) but to process itself (of which main() is just the entry point). In a system with memory protection (where each process is isolated from the others) it's not accessible from outside. You can, however, allocate it in a system specific way to share memory across processes.

我是否必须在程序终止之前释放该数组(分配给"f1"中的数组)(或者在主程序终止时立即释放它)?

Do I have to free the array (allocated in "f1") before the program terminates (or does it get freed as soon as the main program terminates) ?

是的.未分配的内存-在大多数系统中为 -在进程终止时由操作系统自动释放,但这取决于系统.即使在OS上运行IMO,您也应该始终使用自动重新分配作为红旗来进行重新分配(我忘记进行分配了,这是一个错误吗?我错过了一些吗?).此外,如果 f1 被调用1000次,则每次快速耗尽所有可用内存时,它将泄漏内存.考虑服务器中的一个进程,它可能(并且应该)启动并运行了多年.

Yes. Unallocated memory - in most systems - is automatically deallocated by Operating System when process terminates but this is system dependant. IMO even when OS does it you should always deallocate, using such automatic deallocation as a red flag (I forget that to deallocate, is it a bug? something I missed?). Moreover if f1 is invoked 1000 times it'll leak memory each time quickly eating all available memory. Think about a process in a server, it may (and should) be up and running for years.

如果第二个问题的答案为是",那么如何释放在另一个函数中分配的数组?

If the answer for the second question is "yes" then how do you free an array allocated in another function?

当分配内存的人也释放它时,这很好.如果不可能,则呼叫者将负责此类内存.例如,这就是 strdup()的作用.在这种情况下,被调用函数必须(以某种方式)返回指向已分配内存的指针(或可以由另一个专用函数使用的句柄/令牌).例如:

It's nice when who allocates memory also frees it. If it's not possible then caller will become responsible for such memory. It's, for example, what strdup() does. In such case called function must return (somehow) a pointer to allocated memory (or an handle/token that can be used by another specialized function). For example:

char* pBuffer = f1();
// Use it
free(pBuffer);

请注意,如果您想隐藏这样的 internal 指针,可以使用很多技术.您可以使用令牌(例如,整数,字典中的键), typedef 或不透明类型.

Note that there are many many techniques if you want to hide such internal pointer. You may use a token (for example an integer, key in a dictionary), a typedef or an opaque type.

这篇关于函数返回后,在函数中分配的内存是否仍保持分配状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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