函数参数的差异:双指针VS 2D数组

时间:2019-09-02 11:57:04

标签: c arrays function pointers

要理解,在声明的情况下,以下两行在分配上并不等效

double ** v1;
double v2[3][3];

但是什么时候将它们用作函数参数呢?

void func1(double** v1)
void func2(double v2[3][3])

除了关于func2中值的数量的提示外,功能上是否有所不同?

1 个答案:

答案 0 :(得分:1)

严格来说,double **arrdouble arr[3][3]是两种完全不同的类型。

double **arr不是数组,而是指向double的指针。你有  没有更多信息。您无法事先知道arr是双精度数组的数组。因此,进行arr[1][2]的意思是“ 跳过第一个double指针,并将第二个指针取消引用作为一个数组,将第三个元素(索引2)作为对象”

因此,x = arr[1][2]可以翻译为以下伪代码:

tmp = memory[<arr_address> + sizeof(double *)]
x = memory[<tmp_address> + 2*sizeof(double)]
相反,

double arr[3][3]是一个恒定大小的数组,可以进行更多的假设。确保在内存中是连续的,并且执行arr[1][2]的意思是“ 跳过第三个数组double ,然后跳过第二个数组double数组,然后查看第三个(索引2)”。

因此,x = arr[1][2]可以翻译为以下伪代码:

x = memory[<arr_address> + (3 + 2)*sizeof(double)]

作为一个实际示例,请考虑以下程序:

int func1(int** v1) {
    return v1[1][2];
}

int func2(int v2[3][3]) {
    return v2[1][2];
}

int main(void) {
    int const_arr[3][3]; // Values are not important.
    int **double_ptr;    // We don't really run this code!

    func1(const_arr);
    func2(double_ptr);
}

编译后,它会给出以下(非常相关的)警告:

arrayparam.c: In function ‘main’:
arrayparam.c:13:8: warning: passing argument 1 of ‘func1’ from incompatible pointer type [-Wincompatible-pointer-types]
  func1(const_arr);
        ^~~~~~~~~
arrayparam.c:1:5: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
 int func1(int** v1) {
     ^~~~~
arrayparam.c:14:8: warning: passing argument 1 of ‘func2’ from incompatible pointer type [-Wincompatible-pointer-types]
  func2(double_ptr);
        ^~~~~~~~~~
arrayparam.c:5:5: note: expected ‘int (*)[3]’ but argument is of type ‘int **’
 int func2(int v2[3][3]) {

您可以从生成的程序集中非常清楚地看到 ,这两个函数做的事情完全不同,并且该程序集代码实际上与我上面编写的伪代码相同:

func1()

664:   48 89 7d f8             mov    QWORD PTR [rbp-0x8],rdi
668:   48 8b 45 f8             mov    rax,QWORD PTR [rbp-0x8] ; load the array
66c:   48 83 c0 08             add    rax,0x8                 ; go 1*8 = 8 bytes forward (sizeof(int*))
670:   48 8b 00                mov    rax,QWORD PTR [rax]     ; dereference that pointer
673:   8b 40 08                mov    eax,DWORD PTR [rax+0x8] ; go 2*4 = 8 bytes forward (2*sizeof(int)) and take the value there

func2()

67c:   48 89 7d f8             mov    QWORD PTR [rbp-0x8],rdi
680:   48 8b 45 f8             mov    rax,QWORD PTR [rbp-0x8] ; load the array
684:   48 83 c0 0c             add    rax,0xc                 ; go 3*4 = 12 bytes forward
688:   8b 40 08                mov    eax,DWORD PTR [rax+0x8] ; go another 2*4 = 8 bytes forward and take the value there
相关问题