Dynamic Memory Allocation X vector

Asked

Viewed 566 times

7

I was studying this subject in C, and in most of the places I look for, one of the examples of using this resource is when you’re going to create a vector whose size you don’t know. Example:

int main() {

    int num = 0, i = 0, *vetor;

    printf("Digite a quantidade de números que deseja armazenar: \n");
    scanf("%d", &num);

    vetor = (int *) malloc(sizeof(int) * num);

    for(; i < num; i++) {
        printf("Digite um valor inteiro para o %dº elemento do vetor: \n", i);
        scanf("%d", &vetor[i]);
    }

    for (i = 0; i < num; i++) {
        printf("O %dº elemento do vetor é: %d\n", i, vetor[i]);
    }

    free(vetor);

    return 0;
}

But, I can rewrite the program as follows, without using dynamic allocation, and it will continue to work:

int main() {

    int num, i = 0;

    printf("Digite a quantidade de números que deseja armazenar:\n");
    scanf("%d", &num);

    int vetor[num];

    for (; i < num; i++) {
        printf("Digite um valor inteiro para o %dº elemento do vetor: \n", i);
        scanf("%d", &vetor[i]);
    }

    for (i = 0; i < num; i++) {
        printf("O %dº elemento do vetor é: %d\n", i, vetor[i]);
    }

    return 0;
}

What difference does dynamic allocation make in this case? Is there any situation where you have no alternative but to dynamically allocate? And what would be the difference between those two?

vetor = (int *) malloc(sizeof(int) * num);


vetor = malloc(sizeof(int) * num);
  • 2

    No need to do malloc Typecast.

  • 1

    Actually it’s practically a mistake to do this in C. But it’s one of those things that people will never learn because it’s full of example teaching wrong.

  • Even better that apontador = malloc(elementos * sizeof (tipo)) is apontador = malloc(elementos * sizeof *apontador) because you don’t need to check if the type matches the defined type.

1 answer

7


The dynamic allocation call is made in heap. This has implications on possible lifetime of the object. We are on this site, it survives the end of the function, it does not burst or overload the application stack and gives more control even when it is available.

In general it is always possible to allocate dynamically, only it is not desirable, it has several problems that can be seen in links here posted. This is a choice that must be made sparingly, only when it is really necessary and brings advantages.

In this specific example there is a reason to use dynamic allocation: the object may be too large. When the size is unknown by the programmer it must go the safest way. And in this specific case dynamic allocation does not bring real disadvantages (I don’t know if I would say the same if the example changes, even if little). It won’t give any problems in the normal tests that someone would do with a few elements, but with a million it probably would. Already 1 billion in some cases may give error in dynamic allocation as well.

See more:

Browser other questions tagged

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