0

Can you tell me where i have to put the free() function for the array "arr" in this code? I tried to put free(arr) before the last printf() but in this way my output is wrong. I'm trying also to check if the memory allocated is free.

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;
}
Rosen Dimov
  • 1,055
  • 12
  • 32
  • Rule of thumb: every pointer you've got via malloc/calloc/realloc must be freed via free once you're done with the pointer. – Jabberwocky Aug 07 '20 at 11:36
  • What's the use of `a = (int*) realloc(arr,n*sizeof(int)+sizeof(arr));` in `boh`? You reallocate and you throw away the `a` pointer, this is wrong and pointless. I think you want rather `arr = (int*) realloc(arr,n*sizeof(int)+sizeof(arr));` – Jabberwocky Aug 07 '20 at 11:38
  • @Jabberwocky: `arr = … realloc(arr,…);` is bad code because it discards the previous value of `arr`, which should be retained in case the reallocation fails. – Eric Postpischil Aug 07 '20 at 12:43
  • @EricPostpischil yes of course it's bad, but not quite as bad as the OP's original code. – Jabberwocky Aug 07 '20 at 12:43

2 Answers2

1

Can you tell me where i have to put the free() function for the array "arr" in this code?

There is no appropriate place for your main program to free() pointer arr because function boh() reallocates the block to which it points. Thereafter, the pointer you have responsibilty to free is the one returned by realloc(), not the original one. 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.

These corrections are required:

  • check the return value of every malloc() and realloc() (and calloc()) call. 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.

  • After verifying that realloc()'s return value is not null, your function boh must use that value instead of the original value of arr. A fairly easy way to do that would be simply to assign it to arr before the for loop:

         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.

  • In the main program, free b after you're done using it (that is, just before the return).

I'm trying also to check if the memory allocated is free.

C does not provide any way to test the allocation status of a pointer. 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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
-1

I think your program has a few flaws.

Here's a version without memory leaks:

#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.

A problem I detected is that you were using realloc without passing the actual size of the array.

Using sizeof(arr) won't work, you must pass it as a parameter to the function.

Luca
  • 322
  • 4
  • 19