1

I am quite new in C and I have a question about understanding one memory allocation issue:

So lets say I allocate memory for an int array:

 int* my_array = malloc(sizeof(int)*10);

Now I know that a memory block of the given size has been allocated at position '&my_array[0]' (or just my_array).

SO:

If I know call a method to fill my array, I KNOW I should give as a parameter the pointer to my array in order to fill it, so something like:

void fill_array(int* array) {//..do something}
fill_array(my_array);

But I was wondering, what would happen, if the method itself allocates another memory block and I then try to set those equal, so what I mean is:

int* get_filled_array() {

    int* result_array = malloc(sizeof(int)*10);
    //fill it somehow...

  return result_array;
}

And then I set it like:

 int* my_array = malloc(sizeof(int)*10);
 my_array = get_filled_array();

Does the memory of my_array gets removed or what is happens? I quess it is wrong this way but was just wondering. I quess if I want to do it like this, I should create an temporary array, get the returned array of (get_filled_array() and then set my_array and the temporary equal, so I can later free the memory of both?

3 Answers 3

4

If you do

my_array = malloc(...);
my_array = ...;

The old value of my_array (the address of the block allocated by malloc) is overwritten and lost. This is called a "memory leak" (it can't be freed because the program doesn't know where it is anymore).

If you want to do it like that, just use

int *my_array = get_filled_array();

There's no need to call malloc twice.

Sign up to request clarification or add additional context in comments.

Comments

3

There will be a memory leak because the address of the memory allocated in statement

int* my_array = malloc(sizeof(int)*10);

will be lost after this assignment

my_array = get_filled_array();

And the memory previously allocated can not be freed.

Thus before calling get_filled_array you have to free the already allocated memory

int* my_array = malloc(sizeof(int)*10);
//...
free( my_array );
my_array = get_filled_array();

Comments

0

Render unto free(.) everything malloc(.) rendered unto thee.

That is, you must call free(.) on anything you got from malloc(.).

Your code fails to free(.) the pointer returned by the first malloc(.)

Code like this:

 int * my_array = get_filled_array();

 //All code using `my_arrray` must in or called from this section...


 free(my_array);

Consider calling the function malloc_filled_array(.) so its action in allocating memory is obvious.

If you're just zero filling you will probably prefer to use calloc(.) that does it for you and on many platforms will be more efficient if that matters.

NB: You also need to call free(.) on values returned by calloc(.) and realloc(.).

Finally, it can sometimes be acceptable to just let the program end without calling free(.) and you're assured the run-time / operating system will just recover any allocated memory during termination. If you do that in a program that has a loop it might keep allocating more memory and run out even though it isn't really using everything it allocated.

It is not a recommended strategy unless absolutely necessary as it tends to mask leak problems with the program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.