Kamel Fakih
Kamel Fakih

Reputation: 47

does using malloc( ) inside a function free the allocated memory after its execution?

when trying to print the values of struct variables after the function returns it prints some random text (which I think is due to memory allocation error)

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

typedef struct {
    char s1[20];
    char s2[20];
    int n1;
} TEST;

void allocate(TEST *T, int n){
    T = malloc(sizeof(TEST)*n);
    for(int i=0; i<n; i++){
        sprintf((T+i)->s1, "string 1 of %d", i);
        sprintf((T+i)->s2, "string 2 of %d", i);
        (T+i)->n1 = i;
    }
}

int main(){

    TEST *T;
    int n = 3;
    allocate(T, n);
    for(int i=0; i<n; i++){
        printf("%s\n%s\n%d\n\n", (T+i)->s1, (T+i)->s2, (T+i)->n1);
    }

}

Upvotes: 1

Views: 94

Answers (5)

M.M
M.M

Reputation: 141648

The program causes undefined behaviour by passing uninitialized T to the function. Furthermore you never return the new pointer value from the function back to main.

The pointer to new memory is an output of the function, not an input. So it should be a return value, not a parameter. For example:

TEST* allocate(int n)
{
    TEST* T = malloc(sizeof(TEST)*n);
    // etc.
    return T;
}

and then in main:

TEST* T = allocate(n);
// ... use T ...
free(T);

Upvotes: 1

aviraldg
aviraldg

Reputation: 9164

No. The problem with your program is that T is passed by copy to allocate, and the address to the allocated memory is assigned to this copy. To fix, you could make the first allocate parameter **T, pass in the address of T in main, and dereference the pointer to pointer in allocate and assign to it.

Upvotes: 1

&#212;rel
&#212;rel

Reputation: 7662

As said by the already given answers you need to call free to free the memory.

You want to allocate the pointed memory area, so you need a TEST**:

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

typedef struct {
    char s1[20];
    char s2[20];
    int n1;
} TEST;

void allocate(TEST **T, int n){
    *T = malloc(sizeof(TEST)*n);
    for(int i=0; i<n; i++){
        sprintf((*T+i)->s1, "string 1 of %d", i);
        sprintf((*T+i)->s2, "string 2 of %d", i);
        (*T+i)->n1 = i;
    }
}

int main(){

    TEST *T;
    int n = 3;
    allocate(&T, n);
    for(int i=0; i<n; i++){
        printf("%s\n%s\n%d\n\n", (T+i)->s1, (T+i)->s2, (T+i)->n1);
    }
    free(T);
}

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234875

No, C absolutely does not call free automatically for you.

The issue in your program is that T in the caller to allocate is not changed. C is strictly a pass by value language.

One solution is to change the type of T to TEST** in allocate:

void allocate(TEST **T, int n){

with

allocate(&T, n);

in main. You then call free in main.

Upvotes: 4

AKX
AKX

Reputation: 169387

No, it doesn't. You need to free() malloc()ated memory yourself.

Your program, as is, leaks memory... but for this particular program it doesn't matter an awful lot since that memory is freed when the process dies.

Upvotes: -1

Related Questions