简体   繁体   English

C中的内存泄漏(malloc)

[英]Memory leak in C (malloc)

to learn Heap memory, I used the following code.为了学习堆内存,我使用了以下代码。 I used malloc inside a called function (fn1), and for some I reason, I decided not to free the memory inside the called function (fn1).我在被调用函数 (fn1) 中使用了 malloc,出于某种原因,我决定不释放被调用函数 (fn1) 中的内存。 I passed the address of the random alocated memory as return to the calling function(fn2).我将随机分配内存的地址作为返回传递给调用函数(fn2)。 So after using the data from the heap memory in called function(fn2), can I free the malloc memory outside the called function(fn1)?那么在调用函数(fn2)中使用堆内存中的数据后,我可以在调用函数(fn1)之外释放malloc内存吗?

#include <stdio.h>
#include <stdlib.h>

int *add ( int*a, int*b)
{
    int *c = (int*)malloc(sizeof(int));
    *c = (*a)+(*b);
    return c;
}

void main()
{
    int a=2, b=3;
    int *s = add(&a,&b);
    printf("The sum is: %d\n", *s);
    free(s);
}

In the above code, I'm returning the dynamically allocated value c as function return and storing it in s .在上面的代码中,我将动态分配的值c作为函数 return 返回并将其存储在s中。 Will free(s);免费; clear the space in heap memory?清除堆内存中的空间?

Will free(s);免费; clear the space in heap memory?清除堆内存中的空间?

free(s); will release the memory reservation.释放内存保留。 The memory is generally made available for other use.内存通常可用于其他用途。 You can free memory anywhere in the source code.您可以在源代码中的任何位置释放内存。 It does not need to be in the same routine that allocated it.它不需要在分配它的同一例程中。 malloc and free are intended for dynamic memory allocation, meaning it is controlled by the running program, not by the compiler or any compile-time constraints such as location in the source code. mallocfree用于动态内存分配,这意味着它由正在运行的程序控制,而不是由编译器或任何编译时约束(例如源代码中的位置)控制。

free(s); generally does not clear the space.一般不会清理空间。 The C standard does not require an implementation to clear the memory. C 标准不需要实现来清除内存。 Common behaviors include leaving the memory unchanged until it is reallocated, using the memory to help manage the pool of available memory, and, as a debugging feature when requested, “scribbling” patterns of data into the memory to make it more likely that any incorrect use of the memory will cause noticeable symptoms in the problem (rather than letting a bug go undetected).常见行为包括在重新分配内存之前保持内存不变,使用内存来帮助管理可用内存池,以及在请求时将数据模式“涂鸦”到内存中,以使其更可能出现任何不正确的使用内存会导致问题出现明显的症状(而不是让错误未被发现)。

It's ok !没关系 ! You can free memory outside of the function that creates it, but that's considered bad practice.您可以在创建它的函数之外释放内存,但这被认为是不好的做法。 If the consumer of the 'add' function can't get the source code, it can't know you're using malloc.如果'add'函数的使用者不能得到源代码,它就无法知道你正在使用malloc。 To solve this problem, you have to pass it as an argument and avoid using malloc in the 'add' function:要解决此问题,您必须将其作为参数传递并避免在“添加”函数中使用 malloc:

int *add ( int*a, int*b, int* res)
{
    *res = (*a)+(*b);
    return res;
}

void main()
{
    int a=2, b=3;
    int *c = (int*)malloc(sizeof(int));
    int *s = add(&a,&b, c);
    printf("The sum is: %d\n", *s);
    free(s);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM