Why did a dynamic array work without the use of malloc()?

Asked

Viewed 295 times

5

Follow the code passage below:

int main(void)
{
    int tam, vet[tam];
    printf("\ndigite tam: ");
    scanf("%d", &tam);
    return 0;
}

I didn’t know this worked, because I’m typing the vector size at runtime. For me, this was one of the features of malloc(), because in this case, I expected the user input, I took the typed size and would make a malloc() with this size. But from the looks of it, C already does it automatically?

What happens in this case? What would be more appropriate to do in cases like this?

2 answers

9


Your code does not allocate as you imagine, you are making a serious mistake. This code is far from doing what you think it is.

You’re reserving memory for vet with an undetermined amount of elements. Yes, because you declared tam but did not put any value to it. So the value that is in the memory position of tam will be the size of vet. Certainly the information that is there is garbage. It may be that it is zero and does not reserve memory for vet. It may be a very large number and reserve several KB or MB of memory without you needing it.

You only get one value for tam after the variable vet is declared.

What you may not have understood is that C lets you do whatever you want with memory. If it works by coincidence, poor your application. In C you have to make sure everything is working properly. Taking a test and thinking it’s working is a danger. It worked in a specific situation. The next execution may not work, the execution on another computer may not work. Although this is especially important in C, it is something that applies to any language. Testing and working does not guarantee that the program is correct, especially in C where there is much undefined behavior (answer here). Unfortunately there is widespread thinking that thinks a simple test is enough.

A possible correct implementation:

#include <stdio.h>

int main(void) {
    int tam;
    printf("\ndigite tam: ");
    scanf("%d", &tam);
    int vet[tam];
    printf("%d", vet[0]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that I am just doing what you intended. I am not storing anything in this array.

See a example on Wikipedia how to allocate in stack.

4

Arrays with variable size are allowed from the C99, as you can see here.

According to the same link, these arrays function like any other of size which is a constant expression, so it has the same functioning of

int v[10];

In addition, they do not function as an array allocated with malloc, since these are allocated in the heap as long as the arrays variable size are allocated to the stack, but with unknown address at compile time.

  • 1

    Then the C99 allows you to declare variables in how much the program runs ? C99 would be higher than the C90 and C89

  • Voce is not declaring variables while the program runs, Voce is determining the size of the array that is already declared and will be created at execution time.

  • 1

    C99 does not allow variable declaration at runtime. It allows the size of a vector to be determined at runtime.

  • 3

    @Lucasvirgili worth complementing that as tam was not initialized at the time of variable declaration vet, we have a undefined behavior in this code. Compiling with the flag -Wall gcc must display the unchecked variable Warning.

  • ah yes, I ignored his code to answer the question. in fact the vector has indefinite size.

Browser other questions tagged

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