比較這兩個代碼:
void foo(int rows, int cols, int **ar)
{
printf("%d\n", ar[rows - 1][cols - 1]);
}
和
void foo(int rows, int cols, int ar[rows][cols])
{
printf("%d\n", ar[rows - 1][cols - 1]);
}
為了
int main()
{
int ar[3][2] = {{1, 2}, {3, 4}, {5, 6}};
foo(3, 2, ar);
}
第一個 foo,只有雙指標,程式終止。指定尺寸的第二個列印正確的結果。這是為什么?陣列不是作為函式指標傳遞的嗎?
根據程式集輸出,兩者都會導致相同的結果。重點是計算從陣列開頭的偏移量。從程式集中,第一個 ( 1) 數字存盤在-32(%rbp),所需結果 ( 6) 存盤在-12(%rbp)。所以這兩個程式集都導致結果-32(%rbp) 20(經過計算)。
第一次組裝:
.text
.section .rodata
.LC0:
.string "%d\n"
.text
.globl foo
.type foo, @function
foo:
endbr64
pushq %rbp #
movq %rsp, %rbp #,
subq $16, %rsp #,
movl
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/340108.html
上一篇:std::vector和移動語意
下一篇:std::vector和移動語意
