Problem with vector with dynamic allocation

3

The intention of this program I am doing is to serve as if it were a banking system in which the amount of accounts to be created do not have a previously defined amount. In main() , I call the function inserir() , and print it right away to see if it worked. Which was not the case.

The program runs but hangs when I put it to print. What could be wrong?

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

int ler_dados(){
    int cod;

    printf("Digite o codigo:");
    scanf("%d",&cod);

    return cod;
}

void inserir(int *cod, float *saldo, int *TAM){
    int conta;

    conta = ler_dados();

    cod = (int*) malloc(sizeof(conta)*(*TAM));
    saldo =(float*) malloc(sizeof(float)*(*TAM));
    cod[*TAM-1] = conta;
    saldo[*TAM - 1] = 0;
    *TAM++;
}

int main (void){
    float *saldo;
    int *cod, TAM,i;

    TAM = 1;

    for (i=0; i<TAM; i++) {
        inserir(cod, saldo, &TAM);      

        printf("Vetor");

        for (i=0; i<TAM; i++) {
            printf("%d",cod[i]);
        }
    }   
}
    
asked by anonymous 30.08.2018 / 15:59

1 answer

2

The whole idea is wrong, I have improved some things and left others aside.

If there is an entity bank account should create a type to deal with it, it should not deal with members of this alone as if they were isolated things. I used more meaningful names and within the normal naming pattern, keeping everything in the lower case and using the plural vector to make it clear that there are several elements.

Initialize the vector in main() and I will release the memory at the end, although in this case I do not need it because it is really running out.

I've done a size control in a simpler way, avoid passing things by reference unnecessarily. And I used the size as a trigger to close the registry, in the original code it was infinite.

I used realloc() to change the location allocation. This is not ideal, but it is better to go allocating new vectors and abandoning memory messing up which would create future problems. I kept increasing one by one, but this is not correct, should allocate a minimum number of initial items and go at least doubling in size each time it does the relocation, but would need to do the relocation only when the capacity is all filled, otherwise it could be even worse. As already has size and capacity the most correct would be to have a type that abstracted the vector of accounts with these elements, alias this is what is done in real code, I just did not want to change the code too much.

I want to remember that float is not suitable for storing monetary value, but I left it as an exercise and would complicate doing it right. But keep this in mind when doing something real.

I cleaned the code. Not ideal, for exercise I think it's good to have an idea.

The code should probably not be typed but rather taken from an atomic source.

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

typedef struct {
    int codigo;
    float saldo;
} Conta;

int ler_dados() {
    int cod;
    printf("Digite o codigo:");
    scanf("%d", &cod);
    return cod;
}

int inserir(Conta *contas, int tamanho) {
    int codigo = ler_dados();
    if (codigo == 0) return tamanho;
    contas = realloc(contas, sizeof(int) * (tamanho + 1));
    contas[tamanho].codigo = codigo;
    contas[tamanho].saldo = 0;
    return ++tamanho;
}

int main (void) {
    Conta *contas = malloc(sizeof(int));
    int tamanho = 0;
    while (1) {
        int retorno = inserir(contas, tamanho);
        if (retorno == tamanho) break;
        tamanho = retorno;
    }
    printf("Vetor\n");
    for (int i = 0; i < tamanho; i++) printf("%d\n", contas[i].codigo);
    free(contas);
}

There is a problem with this code, see more on Dynamic Allocation Problem - realloc () .

    
30.08.2018 / 16:34