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];
8
  • Can you explain why you believe "dynamic allocation can significantly impact how long it takes for code to execute"? Commented Jan 23, 2022 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 Commented Jan 23, 2022 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...). Commented Jan 23, 2022 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? Commented Jan 23, 2022 at 19:27
  • 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. Commented Jan 23, 2022 at 19:47

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.