ProgrammingModerate

Which declaration makes a 10^6 int matrix at run time without depending on the stack size?

  1. A. int *m = malloc((size_t)1000 * 1000 * sizeof *m);
  2. B. int m[1000000];
  3. C. int m[1000][1000];
  4. D. int m[1000000] = {0};

Correct answer: A. int *m = malloc((size_t)1000 * 1000 * sizeof *m);

Automatic arrays live on the stack, which is limited in size. malloc allocates from the heap, and the expression multiplies in size_t to avoid overflow.

More C Programming questions

Exams that ask this topic