Skip to content
Official PYQ · 2014

GATE CS 2014 Set 2 Q34 — Intermediate Code Generation

Explain with AI

For a C program accessing X[i][j][k]\mathbf{X[i] [j] [k]}, the following intermediate code is generated by a compiler. Assume that the size of an integer is 3232 bits and the size of a character is 88 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
View worked solution

Answer: A

In C, multi-dimensional arrays are stored in row-major order. For a 3D array declared as type X[D1][D2][D3], the byte offset of the element X[i][j][k]X[i][j][k] from the base address is calculated as:

Offset=(i×D2×D3+j×D3+k)×w\text{Offset} = (i \times D_2 \times D_3 + j \times D_3 + k) \times w Offset=i×(D2×D3×w)+j×(D3×w)+k×w\text{Offset} = i \times (D_2 \times D_3 \times w) + j \times (D_3 \times w) + k \times w

where ww is the size (in bytes) of each element of the array.

Let us compare this formula with the given intermediate code:

  • t0 = i * 1024   ⟹  D2×D3×w=1024\implies D_2 \times D_3 \times w = 1024
  • t1 = j * 32   ⟹  D3×w=32\implies D_3 \times w = 32
  • t2 = k * 4   ⟹  w=4\implies w = 4

Step 1: Determine the data type

Since w=4w = 4 bytes, and the question specifies that an integer is 3232 bits (44 bytes) and a character is 88 bits (11 byte), the array XX must be of type int. This eliminates options C and D.

Step 2: Determine the dimensions

  • Using D3×w=32D_3 \times w = 32 and w=4w = 4: D3×4=32  ⟹  D3=8D_3 \times 4 = 32 \implies D_3 = 8
  • Using D2×D3×w=1024D_2 \times D_3 \times w = 1024: D2×8×4=1024  ⟹  D2×32=1024  ⟹  D2=32D_2 \times 8 \times 4 = 1024 \implies D_2 \times 32 = 1024 \implies D_2 = 32

Thus, the array must be declared with dimensions D2=32D_2 = 32 and D3=8D_3 = 8, which matches int X[D1][32][8].

Looking at the options, Option A (int X[32][32][8]) is the only correct match.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Intermediate Code Generation questions