Free allocated memory before return a function

19,811

Solution 1

As the memory allocated by malloc() is on the heap and not on the stack, you can access it regardless of which function you are in. If you want to pass around malloc()'ed memory, you have no other option than freeing it from the caller. (in reference counting terms, that's what is called an ownership transfer.)

Solution 2

1) Yes, you can free() the malloc'ed memory outside the function

2) No, you cannot free it inside the function and have the data passed outside the function, so you must do 1) here

3) If you're concerned about scarce memory, you need to check for failure from memory allocations always, which you fail to do here, which is then likely to lead to a segfault

Solution 3

Ofcourse you can free the memory allocated in a function outside of that function provided you return it.

But, an alternative would be to modify your function like below, where the caller only allocates & frees the memory. This will be inline with concept of the function which allocates the memory takes responsibility for freeing the memory.

void queueBulkDequeue(queueADT queue, char *pElements, unsigned int size) 
{     
   unsigned int i;     
   for (i=0; i<size; i++)     
   {         
      *(pElements+i) = queueDequeue(queue);     
   }     
   return; 
} 

//In the caller

char *pElements = malloc(size * sizeof(char));
queueBulkDequeue(queue, pElements, size);
//Use pElements
free(pElements);

Solution 4

Yes, you can free memory allocated in a function that you call outside the function; this is precisely what you need to do in this case.

Alternatives include passing a buffer and its length into the function, and returning the actual length to the caller, the way fgets does. This may not be the best alternative, because the callers would need to call your function in a loop.

Share:
19,811
KiL
Author by

KiL

Updated on June 18, 2022

Comments

  • KiL
    KiL almost 2 years

    I am trying to return an array using malloc in a function:

    char* queueBulkDequeue(queueADT queue, unsigned int size)
    {
        unsigned int i;
        char* pElements=(char*)malloc(size * sizeof(char));
        for (i=0; i<size; i++)
        {
            *(pElements+i) = queueDequeue(queue);
        }
        return pElements;
    }
    

    The problem is that I need to free it because my MCU's heap size is limited. But I want to return it so I cannot free it in the function, right?. Can I free the allocated memory outside the function (where I call the function). Is there any best practices for this? Thank you in advance!

    • futureelite7
      futureelite7 almost 12 years
      You will have to free it outside the function. There is no requirement that malloc'd memory must be freed inside the function anyway.
    • R.. GitHub STOP HELPING ICE
      R.. GitHub STOP HELPING ICE almost 12 years
      Of course. This is the entire point of malloc. If you couldn't do this, there would have been no reason for malloc to ever have been created.
  • Greg Hewgill
    Greg Hewgill almost 12 years
    "...a segfault" Or worse, in an embedded system.
  • KiL
    KiL almost 12 years
    yes, I do concern about this issue when you free a malloc'd memory outside the function. How can you fix it?
  • tbert
    tbert almost 12 years
    @KiL the "fix" is to handle the case where your memory allocation routines are unable to allocate the memory you want, and fail gracefully, instead of whatever thread of control is executing getting b0rkened by a NULL pointer.
  • Sergey Kalinichenko
    Sergey Kalinichenko almost 12 years
    Downvoter, please kindly explain what you think is wrong with this answer.
  • George
    George almost 3 years
    Great explanation.