dynamic reallocation - struct array

1

I need to do an exercise in which the code allocates memory as needed, but I need to relocate an array of structs, but I ended up locking in that part.

My Struct is:

typedef struct{
    char nome[100];
    char endereco[100];
    int numero;
    char telefone[11];
    char sexo;
    char cpf[11];
    data nascimento;
    double saldo;
}cliente;

I created the pointer that will get the address of my struct inside the function main() .

cliente *cliente;

Allocated memory to only 1 position of my struct

cliente = malloc(sizeof(cliente));

During the execution of my code, I reallocate the memory to receive another "client", using

cliente = (struct cliente*) realloc(cliente, tam*sizeof(cliente));

But when I use the function to show the registered clients, all appear with memory garbage in the variables, except the last one, what am I doing wrong?

    
asked by anonymous 23.05.2018 / 00:01

1 answer

0

There is nothing wrong with finding garbage after realloc() because this function does not initialize the allocated RAM space.

If you prefer, see calloc() .

    
23.05.2018 / 03:25