Question
For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits.
t0 = i * 1024
t1 = j * 32
t2 = k * 4
t3 = t1 + t0
t4 = t3 + t2
t5 = X[t4]
Which one of the following statements about the source code for the C program is CORRECT?
Official answer and solution
- Official answer
- A
- From
- Official GATE 2014 answer key
Solution
Match the generated intermediate address arithmetic with standard 3D array row-major memory offset formulas.
-
Intermediate Code Analysis:
The generated offset is: -
Standard Row-Major Offset Formula:
For an array declared asType X[D1][D2][D3]with element size bytes: -
Matching Coefficients:
- Coefficient of : bytes.
Since anintis given as 32 bits = 4 bytes andcharis 8 bits = 1 byte, the base type of must beint. - Coefficient of : .
- Coefficient of : .
- The outer dimension does not affect the offset calculation within bounds; among the given options, .
- Coefficient of : bytes.
-
Conclusion:
is declared asint X[32][32][8], which matches option (A).
A
Assuming the outer dimension D1 appears in the offset formula; offset calculation only involves trailing dimensions D2 and D3.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2014 Computer Science and Information Technology (CS) Session 2
- Question
- 44 of 65, printed as CS Q.34
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.