Dynamic allocation problem - realloc ()

3

I'm making a program that works as a payroll system, but I'm having problems in the dynamic allocation part. The program runs only once and stops. In int(main) I put:

    printf("Sizeof : %d\n",sizeof(funcionarios));
    printf("Executou: %d\n",i+1);
    printf("Tamanho: %d\n\n",tamanho);

To try to identify where the error was. In the second execution the sizeof() returns the same value which led me to believe that realloc() did not reallocate the memory. I may be wrong, I'm still learning. I ask for help from someone who is more enlightened than I am.

Complete code below:

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

typedef struct{
    int matricula;
    float salario;
}Funcionario;

int ler_dados(void){
    int dado;
    printf("Digite a matrícula:\t");
    scanf(" %d",&dado);
    return dado; 
}
float ler_salario(void){
      float salario;
      printf("Digite o valor do salário:\t");
      scanf(" %f",&salario);
      return salario;
}
int inserir_funcionario(Funcionario *funcionarios, int tamanho){
    int qntd = sizeof(funcionarios) * (tamanho + 1);
    funcionarios = realloc(funcionarios, qntd);
    funcionarios[tamanho].matricula = ler_dados();
    funcionarios[tamanho].salario = ler_salario();
    tamanho++;
    return tamanho;
}
int main(void){
    setlocale(LC_ALL,"Portuguese");
    int i;
    Funcionario *funcionarios = malloc(sizeof(funcionarios));
    int tamanho = 0;
    for(i=0; i<5; i++)
    {
        printf("Sizeof : %d\n",sizeof(funcionarios));
        printf("Executou: %d\n",i+1);
        printf("Tamanho: %d\n\n",tamanho);
        tamanho = inserir_funcionario(funcionarios, tamanho);
    }
}
    
asked by anonymous 01.09.2018 / 21:12

1 answer

2

The error was up to me in your previous question. I focused on the main problem and let another error go by. But I remember that relocating like this remains inadequate as I said before, and this new problem is the result. Complicates a lot. Memory management should be handled in isolation and preferably in the same place where the variable was created, it is much easier.

This problem occurred because you are passing the memory pointer to be handled, and this allows you to change the data that is in this memory location. But it does not allow you to change the pointer. It may even change, but it will only be seen within the function where it was changed. When it gives realloc() it changes the pointer inside inserir_funcionario() , but outside of it the pointer has not been modified, and there begins the confusion. In the next execution of the function it is receiving the original unplaced pointer, and at some point it will cause corruption problem or even, luckily, break the application.

The solution is to pass to the pointer by reference, then end up having a double pointer in the pass, and you have to derrefer to use it.

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

typedef struct {
    int matricula;
    float salario;
} Funcionario;

int ler_dados(void) {
    int dado;
    printf("Digite a matrícula:\t");
    scanf(" %d", &dado);
    return dado; 
}
float ler_salario(void) {
      float salario;
      printf("Digite o valor do salário:\t");
      scanf(" %f", &salario);
      return salario;
}
int inserir_funcionario(Funcionario **funcionarios, int tamanho) {
    *funcionarios = realloc(*funcionarios, sizeof(Funcionario) * (tamanho + 1));
    (*funcionarios)[tamanho].matricula = ler_dados();
    (*funcionarios)[tamanho].salario = ler_salario();
    return ++tamanho;
}
int main(void) {
    setlocale(LC_ALL,"Portuguese");
    Funcionario *funcionarios = malloc(sizeof(Funcionario));
    int tamanho = 0;
    for (int i = 0; i < 5; i++) {
        printf("Sizeof : %lu\n", sizeof(Funcionario));
        printf("Executou: %d\n", i + 1);
        printf("Tamanho: %d\n\n", tamanho);
        tamanho = inserir_funcionario(&funcionarios, tamanho);
    }
}

See running on ideone . And in Coding Ground . Also put it in GitHub for future reference .

For simplicity you can do this:

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

typedef struct {
    int matricula;
    float salario;
} Funcionario;

int ler_dados(void) {
    int dado;
    printf("Digite a matrícula:\t");
    scanf(" %d", &dado);
    return dado; 
}
float ler_salario(void) {
      float salario;
      printf("Digite o valor do salário:\t");
      scanf(" %f", &salario);
      return salario;
}
int inserir_funcionario(Funcionario *funcionarios, int tamanho) {
    funcionarios[tamanho].matricula = ler_dados();
    funcionarios[tamanho].salario = ler_salario();
    return ++tamanho;
}
int main(void) {
    setlocale(LC_ALL,"Portuguese");
    Funcionario *funcionarios = malloc(sizeof(Funcionario));
    int tamanho = 0;
    for (int i = 0; i < 5; i++) {
        printf("Sizeof : %lu\n", sizeof(Funcionario));
        printf("Executou: %d\n", i + 1);
        printf("Tamanho: %d\n\n", tamanho);
        funcionarios = realloc(funcionarios, sizeof(Funcionario) * (tamanho + 1));
        tamanho = inserir_funcionario(funcionarios, tamanho);
    }
}

See running on ideone . And in Coding Ground . Also I put it in GitHub for future reference .

    
01.09.2018 / 22:56