1,105,625 Community Members

how to free pointers which are to be returned

Member Avatar
grvs
Junior Poster in Training
70 posts since Jun 2008
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I allocated memory to a pointer, which i had to return. So I don't know where to free it as its a local variable. I was trying to create an array of variable length (user-specified). I got some help for that in this forum only under this thread

here is the code

#include <stdio.h>

void printArray(int arr [], int size);
int * readArray(int size);

int main(){    
    int size;
    printf("Enter the size of the array ");
    scanf("%d",&size);    
    printArray(readArray(size),size);
    getchar();    
    return 0;
}

int * readArray(int size){
    int *A = malloc ( size * sizeof *A );    
    int i;
    for (i=0;i<size;i++){
        printf("\nEnter the element no. %d: ",i+1);
        scanf("%d",(A+i));
    }
    // free(A); this when uncommented cause the first element to be zero, makes sense, but       
    // why other values are not lost
    return A; // Here i return the pointer.
}

void printArray(int *arr, int size){
     int i=0;
     for (i=0; i<size;i++){
         printf("%d  ", *(arr++));
     }
     printf("\n");
     getchar();
}

Should I free it before returning it? If yes then how? Or should I free it in main, will it be known there ?

A sample Output of the program when I free pointer A before returning it is uploaded in image.

Attachments var_len_arr.bmp (759.05KB)
Member Avatar
Luckychap
Posting Pro in Training
451 posts since Aug 2006
Reputation Points: 68 [?]
Q&As Helped to Solve: 62 [?]
Skill Endorsements: 0 [?]
 
0
 

its always better to free allocated memory after it has been used, so you should free it after printing them ie in the print-array function.

Member Avatar
Aia
Nearly a Posting Maven
2,307 posts since Dec 2006
Reputation Points: 1,977 [?]
Q&As Helped to Solve: 236 [?]
Skill Endorsements: 20 [?]
 
0
 
int * readArray(int size){
    int *A = malloc ( size * sizeof *A );    
    int i;
    for (i=0;i<size;i++){
        printf("\nEnter the element no. %d: ",i+1);
        scanf("%d",(A+i));
    }
    // free(A); this when uncommented cause the first element to be zero, makes sense, but       
    // why other values are not lost
    return A; // Here i return the pointer. /* What are you returning here if you freed it before? */
}

You want to free memory when you don't need it anymore. So, thinking this logically, you want to return a pointer of that memory to the calling function in main. If you freed that memory you are passing a pointer to some unpredictable piece of memory.
When you free some memory using function free(), it doesn't mean that memory automatically is zeroed out, it means now that memory is available for new use.
In essence your function may or may not return the right value, you left it to chance, since it could have been altered already.

[edit:] I leave for you to figure where then you need to use free()

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
0
 

Well in main, you need to do this

int *result = readArray(size);
printArray(result,size);
free( result );
Member Avatar
Aia
Nearly a Posting Maven
2,307 posts since Dec 2006
Reputation Points: 1,977 [?]
Q&As Helped to Solve: 236 [?]
Skill Endorsements: 20 [?]
 
0
 

As an observation to Salem's snippet and as a word of warning. int *result = readArray(size); Notice that he declares a pointer in main named result that will receive that return from readArray.
If you don't do that, as soon as readArray is finished, that memory allocated using malloc inside readArray will not have any variable connected to it, creating what is known as memory leak; you have lost the ability to free it, therefore it will be unavailable until the program finishes if the operating system is smart enough to free it after the program is done.
You do this kind of situation in a loop a few times and very soon your program will run out of memory and possible will crash.
Be aware of it.

Member Avatar
grvs
Junior Poster in Training
70 posts since Jun 2008
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
int * readArray(int size){  
    // free(A); this when uncommented cause the first element to be zero, makes sense, but       
    // why other values are not lost
    return A; // Here i return the pointer. /* What are you returning here if you freed it before? */
}

well that was one of my question. Why other elements of array are displayed correctly? So may be I'll try to figure it out.

You want to free memory when you don't need it anymore. So, thinking this logically, you want to return a pointer of that memory to the calling function in main. If you freed that memory you are passing a pointer to some unpredictable piece of memory.
When you free some memory using function free(), it doesn't mean that memory automatically is zeroed out, it means now that memory is available for new use.
In essence your function may or may not return the right value, you left it to chance, since it could have been altered already.

May be here are hints for my answer. but If I had left it to chance then, why every time other elements are correctly displayed?

[edit:] I leave for you to figure where then you need to use free()

Well while i was trying to figure out, Salem solved it for me, and you explained, I couldn't stop the temptation to read Salem's and then your post... :-/ so now I can just say thank you both a lot.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article