GATE 2015 CS (CS3) – Question 50
In C, Y has length n and X is n×n, 2≤n≤10. Initially Y is zero and X[i][j]=i+j; all of X lies in one memory page frame. Segment 1 loops i=0,…,n−1 with Y[i]+=X[0][i]. Segment 2 instead uses Y[i]+=X[i][0]. Which are true?
S1: Final Y is the same. S2: X accesses in segment 1 are contiguous. S3: X accesses in segment 2 are contiguous.
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (C) S1 and S2 only
Explanation
Both segments add i to Y[i], since X[0][i]=X[i][0]=i, proving S1. C arrays use row-major storage, so adjacent columns in row zero are contiguous, proving S2. Successive row starts in column zero are n elements apart, so S3 is false. Thus C; sharing a page does not change element adjacency.