Dynamic allocation and runtime of functions

7

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;
    }

}
    
asked by anonymous 17.09.2015 / 15:50

2 answers

5

The memory actually remains allocated at the end of the function execution. To clean it use free , as has already been said by @Rrigo Vieira. To be able to manipulate the elements allocated in memory within main there are two alternatives. Declare a global pointer of type cel and make it point to p within the adicionar function, or, which I think is more feasible to not interfere much with the current implementation, change the type of add function to cel* and return the pointer p :

#include <stdio.h>
#include <stdlib.h>

struct celula{
    int n;
    struct celula *next;
};

typedef struct celula cel;

    int main ()
    {

        cel *p = adicionar();

    }

    cel *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;
        }
        return p;

    }

So you can use p of within main as the "head" of your list.

    
17.09.2015 / 17:55
2

It remains allocated yes, to release you need to use the free.

You need to pass as pointer pointer.

void adicionar(cel** c)
{
}

If you have allocated in main and only want to change some property, just pass as a pointer (*).

Now if you want to allocate within the function you need to pass pointer pointer (**).

Here's an example:

typedef struct aluno{
char *nome;
char *curso; } aluno;

void aloca_aluno(aluno **a,int t);

int main()
{
 aluno *a = NULL;
 aloca_aluno(&a, 1);
 return 0;
}
void aloca_aluno(aluno **a,int t)
{
   //Usando realloc para não perder caso já esteja alocado algo
  if((*a=(aluno*)realloc(*a,t*sizeof(aluno)))==NULL)
  {
   //Não conseguir alocar
     exit(1);
  }
 }
    
17.09.2015 / 16:49