ProgrammingHard
A loop reads n numbers and stores them in int a[n] as a VLA of size up to 10^6 inside main. What is the main risk?
- A. A VLA cannot hold integers
- B. A VLA is slower because it is sorted automatically
- C. The VLA is zeroed, which takes too long
- D. A large VLA lives on the stack, which can overflow, so heap allocation is safer for big sizes
Correct answer: D. A large VLA lives on the stack, which can overflow, so heap allocation is safer for big sizes
Big automatic arrays use the stack, which is limited. malloc or a global array is safer for large sizes.
More C Programming questions
- To sort 10^6 integers with qsort, which comparator is correct for all int values?
- A counting pass over an array of 10^7 values, each in [0, 9], uses 10 counters. Which statement is correct?
- Which output method is fast enough for 10^6 lines of output in C?
- Which 2D DP table declaration is correct for a 1000 x 1000 table of ints in a contest C program?
- A modular sum over 10^6 terms uses sum = (sum + a[i]) % MOD with a[i] < MOD and sum an int. What is wrong?
- A counting loop uses the expression i i <= n with int i and n up to 2^31 - 1. Is the loop correct?