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?

Choose one option
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.

  1. Intermediate Code Analysis:
    The generated offset is:

    offset=t4=1024⋅i+32⋅j+4⋅k.\text{offset} = t_4 = 1024 \cdot i + 32 \cdot j + 4 \cdot k.
  2. Standard Row-Major Offset Formula:
    For an array declared as Type X[D1][D2][D3] with element size SS bytes:

    Byte Offset(i,j,k)=(i⋅D2⋅D3+j⋅D3+k)⋅S\text{Byte Offset}(i, j, k) = (i \cdot D_2 \cdot D_3 + j \cdot D_3 + k) \cdot S
    =i⋅(D2⋅D3⋅S)+j⋅(D3⋅S)+k⋅S.= i \cdot (D_2 \cdot D_3 \cdot S) + j \cdot (D_3 \cdot S) + k \cdot S.
  3. Matching Coefficients:

    • Coefficient of kk: S=4S = 4 bytes.
      Since an int is given as 32 bits = 4 bytes and char is 8 bits = 1 byte, the base type of XX must be int.
    • Coefficient of jj: D3⋅S=32  ⟹  D3⋅4=32  ⟹  D3=8D_3 \cdot S = 32 \implies D_3 \cdot 4 = 32 \implies D_3 = 8.
    • Coefficient of ii: D2⋅D3⋅S=1024  ⟹  D2⋅32=1024  ⟹  D2=32D_2 \cdot D_3 \cdot S = 1024 \implies D_2 \cdot 32 = 1024 \implies D_2 = 32.
    • The outer dimension D1D_1 does not affect the offset calculation within bounds; among the given options, D1=32D_1 = 32.
  4. Conclusion:
    XX is declared as int X[32][32][8], which matches option (A).

Final answer

A

Common trap

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.