When we use some of the C dynamic allocation functions (malloc, calloc, realloc etc) inside a function that is called by main, will the memory remain allocated at the end of the execution of this function or will it automatically be deallocated? If the memory remains allocated, how should I "handle" this previously allocated memory space outside of the function?
Using the code below, for example, I need to use the linked list that I created in add () on main
#include <stdio.h>
#include <stdlib.h>
struct celula{
int n;
struct celula *next;
};
typedef struct celula cel;
void adicionar();
int main ()
{
adicionar();
}
void adicionar()
{
cel *p, *p2, *aux;
int adc, x;
p = NULL;
printf("Deseja adicionar elementos na lista? \n 1- Sim \n 2- Nao \n");
scanf("%d", &adc);
if(adc == 1)
{
do
{
p2 = malloc(sizeof(cel));
printf("Digite o valor que deseja adicionar \n");
scanf("%d", &x);
p2->n = x;
p2->next = p;
p = p2;
printf("Deseja adicionar elementos na lista? \n 1- Sim \n 2- Nao \n");
scanf("%d", &adc);
}while(adc == 1);
}
aux = p;
while(aux != NULL)
{
printf("%d ", aux->n);
aux = aux->next;
}
}