Vector problem with dynamic allocation

Asked

Viewed 75 times

3

The intention of this program that I am doing is to serve as if it were a banking system in which the amount of accounts to be created does not have a previously defined amount. In the main(), i call the function inserir(), and I 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 may 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]);
        }
    }   
}
  • Dude, the intention of this program that I’m doing is to serve as a banking system in which the amount of accounts to be created does not have a previously defined amount. In main, I call the insert function, and print it right after to see if it had worked. Which was not the case. I should have specified better, first time posting something here, patience.

  • There it is much more complex than that, you can even do it in a simplified way, but it would be learning wrong, and this way made, besides giving a mistake, it is inadequate, and causes other problems that are not visible errors, but that is wrong. And will do it endlessly?

  • Erulos, it’s always possible [Edit] the question putting more details and making it clearer :-)

  • Can you tell me what I should study to do this? What I want is a simple banking system, "withdrawal", deposit and delete account. The whole question would be this part of the vector

  • 1

    hkotsubo did it on the topic, and damn it. You edited my post very fast, I saw that it was bugged, I was editing and when I finished. I saw that you had already moved. Thank you

1 answer

2


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

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

I initialized the vector in main() and I released the memory at the end, although in this case I don’t need it because it’s really ending.

I made a size control in a simpler way, avoid passing things by reference without need. And I used the size as trigger to terminate the registration, the original code was infinite.

I used the realloc() to change the location allocation. This is not ideal, but it is better to go allocating new vectors and abandon soiling the memory which would create future problems. I kept adding one to one, but this is not correct, I should allocate an initial minimum number of items and go at least doubling in size each time you relocate, but I would need to relocate only when the capacity is fully filled, otherwise it could be even worse. Since it already has size and capacity the most correct would be to have a type that abstracts the array of accounts with these elements, by the way this is what is done in real code, I just didn’t want to change the code too much.

I want to remind you that float It’s not suitable to store monetary value, but I let it be an exercise and it would complicate doing it right. But keep that in mind when doing something real.

I cleaned up the code. It’s not ideal, for exercise I think it’s good to have an idea.

The code should probably not be typed but 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);
}

I put in the Github for future reference.

There is a problem in this code, see more in Problem with dynamic allocation - realloc().

  • How does this while (1)?

  • It’s infinite. Always true.

Browser other questions tagged

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