Problem with dynamic allocation – realloc()

Question:

I'm making a program that works like a payroll system, but I'm having problems with 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 run, sizeof() returns the same value, which led me to believe that realloc() did not reallocate memory. I could be wrong, I'm still learning. I ask someone more enlightened than me for help.

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

Answer:

The error was mine in your previous question. I focused on the main problem and missed another error. But I remember that relocating in this way is still inadequate as I said before, and this new problem is the result of this. It complicates a lot. Memory management should be handled in an isolated way 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 doesn't let you change the pointer. It can even change, but it will only be seen within the function where it was changed. When you give realloc() it changes the pointer inside inserir_funcionario() , but outside it the pointer has not been modified, and then the confusion begins. In the next execution of the function it is receiving the original pointer not relocated, and at some point it will cause a corruption problem or even, luckily, break the application.

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

#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 working on ideone . And on repl.it. Also posted on GitHub for future reference .

For simplicity, you can do it like 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 working on ideone . And on repl.it. Also posted on GitHub for future reference .

Scroll to Top
AllEscort