typedef int mat[n][n] //(size_t is defined as unsigned long, which is 8 bytes on x84-64 machines)
int ele(mat a, size_t i, size_t j){
return a[i][j];
}
Suppose this generate the following assembly code:
ele:
salq $6, %rsi // i= i<< 6 = 64i
addq %rsi, %rdi // a= a + i = a +64i
movl(%rdi, %rdx, 4), %eax // result = a + 4*j = a + 64i + 4j = a + 8*8*i + 8*1/2*j, since the size(t) = 8
The goal is find n = ____ ? I came up n = 8, you can see my steps in comments. However, the correct answer is n = 16. Can anyone help me find where is the mistake
size_t
is the type of the indexesi
andj
. It's not the type of the array. The pointer arithmetic is being done using the type of the array. BTW, you will never, ever see a1/2
being used in C pointer arithmetic. – user3386109 Nov 8 '18 at 5:47n
and no semicolon at the end of the typedef. It's hard to discuss broken code. Please fix it! – Jonathan Leffler Nov 8 '18 at 5:49sizeof (int)
" – Antti Haapala Nov 8 '18 at 5:54sizeof(int)
is 4 on x86-64 platforms.64 / 4 ==
16
– Antti Haapala Nov 8 '18 at 5:55