in Compiler Design edited by
33,060 views
100 votes
100 votes

For a C program accessing $\mathbf{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?

  1. $\mathbf{X}$ is declared as "int $\mathbf{X[32] [32] [8]}$.
  2. $\mathbf{X}$ is declared as "int $\mathbf{X[4] [1024] [32]}$.
  3. $\mathbf{X}$ is declared as "char $\mathbf{X[4] [32] [8]}$.
  4. $\mathbf{X}$ is declared as "char $\mathbf{X[32] [16] [2]}$.
in Compiler Design edited by
33.1k views

4 Comments

Can it be written like this?

t5 = X[ t4 ]

     = X [ t3 ] [ t2 ]

     = X [ to ] [ t1 ] [ t2 ]

     = X [ i*1024 ][  j *32 ][ k * 4 ]

     = X [ i * ( j * ( k * 4 ) ) ][ j * ( k * 4 )][ k * 4 ]  ( multiplication by 4 is due to 32 bit ( or 4 B) integer )

From here clearly option A) is the answer.
7
7
edited by

$(i\times j\times k+ j\times k+ k)\times 4\ bytes$

$Put\ A)\ values\ "intX[32][32][8]"\ above$

$=(32\times 32\times 8+32\times 8+8)\times 4$

$=33824$


$t5=X[t4]$

$t4=t3+t2$

     $=t0+t1+k\times 4$

     $=i\times 1024+j\times 32+k\times 4$

$Put\ A)\ values\ "intX[32][32][8]"\ in\ 't4'$

$=32\times 1024+ 32\times 32+ 8\times 4$

$=33824$


$Ans: A$

In order to visualize what exactly is happening: Refer here

16
16
Trial and error kind of method:
form the equation for address at A[i][j][k], which will be 1024i+32j+4k.

That makes A[0][0][0], 0.

similarly A[0][1][0] = 32, A[0][2][0] = 64, ie incrementing by 32 implying a 32 bit integer. (C and D eliminated)
Now A[1][0][0] = 1024, which means A[0][j_max][0] – 1024-32 = 992, and 992/32 = 31.
j_max = 31, and 31-0+1, 32 integers.
0
0

9 Answers

0 votes
0 votes

A 3d array defined as a[2][2][2] means that there are 2 2d arrays each of dimension 2x2, i.e., both of them have 2 rows and 2 columns. 

Suppose the array given in the question is A[x][y][z]. This means that there are x 2d arrays each having y rows and z columns.

In the questions, the equations for finding A[i][j][k] can be re written in the form of y and z as follows if the array is assumed to be of type 'int':-

t0 = i ∗ 1024 = i * (y*z) * 4
t1 = j ∗ 32 = j * z * 4
t2 = k ∗ 4 = k * 4

So from the above equations we get,

z*4=32

=> z=8

and y*z*4=1024

=> y=32

So we know that dimensions are A[x][32][8]. Since we assumed int, so A is the answer since it is of this form.

If you assume array to be char then,

t0 = i ∗ 1024 = i * (y*z) * 1 

t1 = j ∗ 32 = j * z * 1 

t2 = k ∗ 4 = k * 4

So from the above equations we get,

z*1=32

=> z=32

and y*z*1=1024

=> y=32

So none of the char options,i.e, options B and C are of the form A[x][32][32]. So A has to be the answer.

Answer:

Related questions