简体   繁体   English

在这种情况下如何使用 free() function? 使用 malloc() 和 realloc()

[英]How use free() function in this case? using malloc() and realloc()

Can you tell me where i have to put the free() function for the array "arr" in this code?你能告诉我我必须把这个代码中数组“arr”的free() function放在哪里吗? I tried to put free(arr) before the last printf() but in this way my output is wrong.我试图将 free(arr) 放在最后一个 printf() 之前,但是这样我的 output 是错误的。 I'm trying also to check if the memory allocated is free.我也在尝试检查分配的 memory 是否免费。

int *boh(int *arr,int n);

int main() {
    int a,i,n;
    int *arr;
    int *b;
    int size = 6;

    arr = (int*) malloc(size* sizeof(int));

    for (i= 0; i<6; i++){
        printf("Give me a number: \n");
        scanf("%d",&a);
        
        if(i != 5){
            arr[i] = a;
        }
        else{
            n = a;
        }
    }


    b = boh(arr,n);


    for(i=0; i<5+n; i++){
        printf("%d\n",b[i]);
    }

    return 0;
}


int *boh(int *arr,int n){
    int *a;
    int i;
    int b;
    b = arr[4];
    a = (int*) realloc(arr,n*sizeof(int)+sizeof(arr));
    
    for(i=5; i<5+n; i++){
        b += b;
        arr[i] = b;
    }
    
    return arr;
}

Can you tell me where i have to put the free() function for the array "arr" in this code?你能告诉我我必须把这个代码中数组“arr”的free() function放在哪里吗?

There is no appropriate place for your main program to free() pointer arr because function boh() reallocates the block to which it points.您的主程序没有合适的位置来free()指针arr ,因为 function boh()重新分配了它指向的块。 Thereafter, the pointer you have responsibilty to free is the one returned by realloc() , not the original one.此后,您有责任释放的指针是realloc()返回的指针,而不是原始指针。 The original pointer must be considered invalid.原始指针必须被视为无效。 The two may in practice have the same value, but often they do not.两者在实践中可能具有相同的值,但通常它们不具有相同的值。

For that reason, your function boh also must not attempt to write through the original value of arr after the reallocation, as it does when it evaluates arr[i] = b .因此,您的 function boh也不得在重新分配后尝试写入arr的原始值,就像在评估arr[i] = b时所做的那样。

These corrections are required:这些更正是必需的:

  • check the return value of every malloc() and realloc() (and calloc() ) call.检查每个malloc()realloc() (和calloc() )调用的返回值。 These functions return a null pointer on failure, in which event you need to accommodate that in some way, such as cleanly terminating the program.这些函数在失败时返回 null 指针,在这种情况下,您需要以某种方式适应它,例如干净地终止程序。

  • After verifying that realloc() 's return value is not null, your function boh must use that value instead of the original value of arr .在验证realloc()的返回值不是 null 之后,您的 function boh必须使用该值而不是arr的原始值。 A fairly easy way to do that would be simply to assign it to arr before the for loop:一种相当简单的方法是在for循环之前将其分配给arr

     arr = a;

    In this case, that will also cause the function to return the new pointer value, which is the one you have responsibility to free.在这种情况下,这也会导致 function返回新的指针值,这是您有责任释放的值。

  • In the main program, free b after you're done using it (that is, just before the return ).在主程序中,使用完后释放b (即在return之前)。

I'm trying also to check if the memory allocated is free.我也在尝试检查分配的 memory 是否免费。

C does not provide any way to test the allocation status of a pointer. C 不提供任何方法来测试指针的分配状态。 The programmer needs to track that themself.程序员需要自己跟踪。 However, you could consider running your program under control of an external leak-checking program such as Valgrind.但是,您可以考虑在外部泄漏检查程序(例如 Valgrind)的控制下运行您的程序。

I think your program has a few flaws.我认为你的程序有一些缺陷。

Here's a version without memory leaks:这是一个没有 memory 泄漏的版本:

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

#define SIZE 6

int *boh(int *arr, int n) {
    int *a;
    int i;
    int b;
    b = arr[SIZE - 2];
    a = (int *) realloc(arr, (n + SIZE) * sizeof(int));
    for (i = SIZE - 1; i < (n + SIZE); i++) {
        b += b;
        a[i] = b;
    }
    return a;
}

int main() {
    int a, i, n;
    int *arr;
    int *b;

    arr = (int *) calloc(SIZE, sizeof(int));

    for (i = 0; i < SIZE; i++) {
        printf("Give me a number: \n");
        scanf("%d", &a);
        if (i != SIZE - 1) {
            arr[i] = a;
        } else {
            n = a;
        }
    }

    b = boh(arr, n);

    for (i = 0; i < (n + SIZE); i++) {
        printf("%d\n", b[i]);
    }

    free(b);

    return 0;
}

You must put a free to the pointers you created with a malloc or similar functions.您必须free使用malloc或类似函数创建的指针。

A problem I detected is that you were using realloc without passing the actual size of the array.我发现的一个问题是您使用realloc时没有传递数组的实际大小。

Using sizeof(arr) won't work, you must pass it as a parameter to the function.使用sizeof(arr)将不起作用,您必须将其作为参数传递给 function。

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

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