Dynamic allocation and runtime of functions

Asked

Viewed 330 times

7

When we use any of the dynamic allocation functions in C (malloc, calloc, realloc, etc.), within a function that is called by main, will the memory remain allocated at the end of the execution of that function or will it automatically be out of focus? If memory remains allocated, how should I proceed to "handle" that previously allocated memory space outside the function?

Using the code below, for example, I need to use the chained list I created in add() in 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;
    }

}

2 answers

5


The memory really remains allocated at the end of the function execution. To clean it use free, as already said by @Rodrigo Vieira. To be able to manipulate the elements allocated in memory within the main there are two alternatives. Declare a global pointer of type cel and make it point to p within the function adicionar, or, what I find more feasible not to interfere too much in the current implementation, change the type of the 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 the p from within the main as the "head" of your list.

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 just want to change some property just pass as pointer (*).

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.