Open In App

GATE | GATE-CS-2014-(Set-2) | Question 44

Like Article
Like
Save Article
Save
Share
Report issue
Report

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?
(A) X is declared as “int X[32][32][8]”.
(B) X is declared as “int X[4][1024][32]”.
(C) X is declared as “char X[4][32][8]”.
(D) X is declared as “char X[32][16][2]”.


Answer: (A)

Explanation: The final expression can be simplified in form ofi, j and k by following the intermediate code steps in reverse order

t5 = X[t4]
   = X[t3 + t2]
   = X[t1 + t0 + t2]
   = X[i*1024 + j*32 + k*4]
   = X + i*1024 + j*32 + k*4 

Since k is multiplied by 4, the array must be an int array.
We are left with 2 choices (A and B) among the 4 given choices.

X[i][j][k]’th element in one dimensional array is equivalent to
X[i*M*L + j*L + k]’th element in one dimensional array
(Note that multi-dimensional arrays are stored in row major order in C).

So we get following equations

j*L*4 = j*32, we get L = 8 (4 is the sizeof(int))
i*1024 = i*M*L*4, we get M = 1024/32 = 32

Therefore option A is the only correct option as M and L are 32 and 8 respectively.



Quiz of this Question


Last Updated : 17 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads