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.

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.

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()

Well in main, you need to do this

int *result = readArray(size);
printArray(result,size);
free( result );

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.