Search code examples
arrayscmultidimensional-arrayembeddedstm32

Does multidimentional array indices calls a function to calculate the element adress in c?


Assume I am working on ARM Cortex M7. Now take a look at:

int a[4][4];
a[i][j]=5;

Is in assembly language a function will be calculating the a[j][j] address or it uses lookuptable (pointer array with the same size) or some magical way to place 5 in correct place?

This is disassembler output:

136               array1[i][i+1]=i;
08000da6:   ldr     r3, [r7, #36]   ; 0x24
08000da8:   adds    r3, #1
08000daa:   ldr     r2, [r7, #36]   ; 0x24
08000dac:   uxtb    r1, r2
08000dae:   ldr     r2, [r7, #36]   ; 0x24
08000db0:   lsls    r2, r2, #2
08000db2:   add.w   r0, r7, #40     ; 0x28
08000db6:   add     r2, r0
08000db8:   add     r3, r2
08000dba:   subs    r3, #36 ; 0x24
08000dbc:   mov     r2, r1
08000dbe:   strb    r2, [r3, #0]

Solution

  • If you write the indices as in your example, the compiler will calculate the exact memory address required at compile time.

    If the indices were variables, then address would be calculated at run time.

    Here is a comparison of assembly output for both cases.