1

I was trying to find out the reason why arrays have static size in C, and so far I know that dynamic allocation can impact how long it takes for code to execute. Also, I know that another reason would be that its size needed to be fixed at compile time, but here comes the problem: What happens when I have something like

int n;
scanf("%d", &n);
int arr[n];

What's the difference if my array size were a static value, just like

int arr[3];
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Can you explain why you believe "dynamic allocation can significantly impact how long it takes for code to execute"? – Retired Ninja Jan 23 '22 at 19:15
  • @RetiredNinja Well, I guess it did not sound what I meant, but, I guess it takes a little bit of time to allocate another block of memory and copy all the elements to that. I may be wrong at this point because i'm still trying to understand – Game changer Jan 23 '22 at 19:19
  • 1
    As an obligatory side note, VLAs (which you have here, Variable Length Array) go to stack. Stack is small. So don't ever create a VLA from external input (like `scanf`) in any real program (or if you do, _check_ that the input value is small enough...). – hyde Jan 23 '22 at 19:24
  • @hyde Hmm, thanks, things now make more sense. I'm still a little bit confused with "it's size need to be fix in compile time", but in the code we do it in run time. Is that statment false? – Game changer Jan 23 '22 at 19:27
  • *"it's size need to be fix in compile time"*, I'm confused about that too. I'm not sure what you mean by it... – hyde Jan 23 '22 at 19:32
  • @hyde I did read that they have to be allocated at compile time, so the compile must know it's size at compile time – Game changer Jan 23 '22 at 19:38
  • Also, VLAs are only guaranteed to even work in `C99`, so it has an effect on which compilers will accept your code. – Neil Jan 23 '22 at 19:39
  • 1
    Array variables in functions are put in stack, which is entirely runtime thing. The stack pointer is adjusted depending on how much space is needed for the array (and other variables). If it is `int a[3]`, there will be code which does approximately "adjust stack pointer by size of 3 ints". If it is `int a[n]`, the code will be "adjust stack pointer by n times sizeof int". Very small difference. – hyde Jan 23 '22 at 19:47

3 Answers3

2

What's the difference if my array size were a static value ... like int arr[3]?

You'll notice the difference when someone types in "1234567890", in which case the variable-length array will overflow the stack. Of course, if you try to define an overly large static array, you'll also get a stack overflow.

The performance question surely can't matter, since any code in which you parse formatted I/O is not going to be in your performance-critical tight loop. If you were to get n from a function parameter, then there are probably cases where the compiler's ability to know the size of your data could impact its ability to apply various optimizations. For example, if you were to call memset() on your fixed-size array, the compiler might well be able to replace that with one or a few machine instructions rather than the full code of memset(), efficient though it might be.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

int arr[n]; it is not dynamic allocation. Dynamic allocation happens when you use malloc family of functions.

What's the difference if my array size were a static value, just like

int arr[3];

There is almost no difference from the performance perspective (a few more machine code instructions.

Bear in mind that VLAs can be only defined in the function (or more general block) scope (ie they have to have automatic storage duration). They cannot also be initialized when defined.

0___________
  • 60,014
  • 4
  • 34
  • 74
1

In this code snippet

int n;
scanf("%d", &n);
int arr[n];

there is declared a variable length array. Its size is determined at run-time according to the entered value of the variable n.

Pay attention to that the value of the variable n shall be greater than zero.

You may not initialize such an array in its declaration. And such an array may be declared only in a block scope. That is the array shall have automatic storage duration.

In this declaration

int arr[3];

there is declared an array with a fixed size. You may initialize it in its declaration like for example

int arr[3] = { 0 };

Such an array may be declared in a file scope or a block scope.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335