CrispyCashew CrispyCashew - 1 month ago 5
C Question

Avoiding Memory Leaks when passing pointers to functions

Im reading up how to avoid memory leaks and to my understanding , a memory leak occurs if you lose anyway to refer to allocated memory (therefore you cant free it) but this example from Wikipedia confuses me

#include <stdlib.h>

void function_which_allocates(void) {
/* allocate an array of 45 floats */
float *a = malloc(sizeof(float) * 45);

/* additional code making use of 'a' */

/* return to main, having forgotten to free the memory we malloc'd */
}

int main(void) {
function_which_allocates();

/* the pointer 'a' no longer exists, and therefore cannot be freed,
but the memory is still allocated. a leak has occurred. */
}


My code is below, what Im doing is creating an array of structures called arrayOfStrucs that hold structures called CreatedStruct, then a LinkedList points to each instance of arrayOfStrucs.

Will I get memory leaks as I dont free memory I allocated in the same function ie CreateLinkedList ( i allocate arrayOfStructs but dont free it as I still need it to be pointed by a Linked List) To me im not creating any orphaned pointers as im always able to reference them at any time

struct LinkedList *temp = head;
CreatedStruct* StructPtr = &temp->arrayOfStruct[1];
int* intPtr = StructPrt->data;
printf("int data = %d\n", *intPtr);


With the code above I can print the value of arrayOfStruct ( so the only data member in CreatedStruct which is int* data) so dosent that mean it wont leak?
Does the Wiki example cause a leak because function_which_allocates(); didnt return a reference to the allocated memory, or is it because the allocation and freeing of the memory were in different functions ( different scopes)

int main(void)
{
struct LinkedList *head =NULL;
head = CreateLinkedList(head)
printLinkedList(head)
//Can this be done if I follow the correct way of freeing a linked list?
FreeLinkedList(head)
}

LinkedList* CreateLinkedList( LinkedList* head)
{
CreatedStruct* arrayOfStrucs;
//Allocated but will be freed in a separate function
arrayOfStructs= malloc(1 * sizeof(CreatedStruct));
FillArrayOfStructs(arrayOfStructs);
head = FillLinkedList(head, arrayOfStrucs);
return(head)
}


FillArrayOfStructs(CreatedStruct* arrayOfStructs)
{
arrayOfElements[1].data = malloc( sizeof(int) );
ptr = arrayOfElements[1].data;
*ptr = 65;
}


LinkedList* FillLinkedList(LinkedList* head, CreatedStruct* arrayOfStructs)
{
LinkedList* insertNode = malloc(sizeof(LinkedList));
insertNode->arrayOfStruct = (void*)arrayOfStructs;
insertNode->next = head;
return insertNode;
}


Summary

Can memory leak be caused by not allocating and then freeing memory in the same function scope ( ie allocated in main, which main then passes the pointer to a seperate freeing function)

Answer

Every memory block obtained via malloc and friends that is not freed with the free function will yield in a memory leak and this is toally independent of scopes.

In other words, you may allocate a memory block in some function and free it later in a totally different function; this is actually how malloc and free are used most of the time.

Example

char *Foo()
{
   ...
   return malloc(...);
}

void Bar()
{
  char *p = Foo(); 
  ...
  free(p);   // freeing the pointer that has been allocated in Foo
}

The Wiki example causes a leak because once the function has been executed, the a pointer doesn't exist any more because it is a local variable that exists only during the execution time of the function.

But the memory block itself that has been allocated and that was pointed by a is still allocated and now there is no more way to free it because the only pointer (a) that pointed to it is gone.