ProgrammingModerate
Which declaration makes a 10^6 int matrix at run time without depending on the stack size?
- A. int *m = malloc((size_t)1000 * 1000 * sizeof *m);
- B. int m[1000000];
- C. int m[1000][1000];
- 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
- For a matrix of size n x n with n = 500, which algorithm is correct and fast enough within 1 second for C multiplication?
- Given int a[3][4] and int q = &a[0][0], which element does q[5] refer to?
- int m[2][3] = {{1}, {2, 3}}; What is m[0][0] + m[1][0] + m[1][1] + m[0][2]?
- int a[3][4]; what do sizeof a / sizeof a[0] and sizeof a[0] / sizeof a[0][0] give?
- char g[3][4] = {"abc", "de", "f"}; What is the value of g[1][2] when printed with %d?
- int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; How many monotone paths (moves right or down) are there from m[0][0] to m[2][2]?