2
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    int L[n];
    // ...
    return 0;
}

I'm asking if I can give the number of elements in an array as needed using a variable . The teacher told us that this method is not recommended and I did not understand why. Thanks for answering for my question .

TheKing
  • 136
  • 9

1 Answers1

4

This declaration

int L[n];

is a declaration of a variable length array. Variable length arrays are conditionally supported by compilers.

So it is not excluded that you can meet a compiler that does not support such declarations.

Pay attention to that in this declaration the value of the variable n shall be greater than 0.

As such an array has automatic storage duration then its size should not be vary big. Otherwise you should allocate memory for the array dynamically.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I don't know what conditionally means , but i have a qst : why we use the malloc fuction if variable lenght array are supposted by compilers ? – TheKing Apr 14 '22 at 21:48
  • @TheKing Supporting conditionally means that it is not necessary that the compiler supports variable length arrays. For example if the system macro __STDC_NO_VLA__ is defined equal to 1 then it means that the compiler does not support VLAs. – Vlad from Moscow Apr 14 '22 at 21:49
  • ok thanks , but does it depend on the version of the compiler? – TheKing Apr 14 '22 at 21:52
  • @TheKing It can depend why not? – Vlad from Moscow Apr 14 '22 at 21:53
  • 1
    @TheKing - Compilers prior to C99 do not support VLA. Some compilers since C99, including C99 support VLA. But not all of them. That is what is meant by conditionally here. – ryyker Apr 14 '22 at 21:55
  • ok , last qst , why we use malloc function if we can use this method? – TheKing Apr 14 '22 at 21:55
  • @TheKing - VLA is on the stack. malloc is heap. Duration and usage needs dictate which is optimal. Portability for example would suggest using malloc. But ease of programming sometimes make VLA very desireable. Read as much as you can about both, and make the decision based on your specific needs – ryyker Apr 14 '22 at 21:56
  • @TheKing VLAs have automatic storage duration. That is they allocate memory in the stack. So if an array is very big then the stack memory can be exhausted. – Vlad from Moscow Apr 14 '22 at 21:57
  • Ok , thank u for answering my qsts :) – TheKing Apr 14 '22 at 21:58
  • Someone already said that stack vs heap is important but maybe didn't explain why. When your variable is on the stack, its memory exists for the duration of the function it is in -- since your example is in `main`, that's forever, but if you move this code into a helper function then the memory will only last while the helper is in the stack trace and it would be easy to have passed that pointer to something else and cause a crash. When you allocate on the heap, you have to remember to free it, but you can also access the memory throughout. That's a long version of "Duration and usage needs... – Alex Nicolaou Apr 14 '22 at 22:09