Why does C disallow an array return type?

前端 未结 3 703
无人共我
无人共我 2021-01-15 20:58

Why is this valid in C

int * foo(int a,int b){
...
}

but this is invalid

int [] foo(int a,int b){
...
}
3条回答
  •  一整个雨季
    2021-01-15 21:36

    The syntax is somewhat funny.

    int foo(int a, int b) []
    {
        ...
    }
    

    But it's not allowed anyway. See n1256 6.7.5.3 paragraph 1, "Function declarators".

    A function declarator shall not specify a return type that is a function type or an array type.

    You can return pointers to arrays:

    int (*foo(int a, int b)) []; // Weird syntax, n'est-ce pas?
    

    But you might as well just return a pointer instead, since the following are equivalent:

    int array[] = { ... };
    int *x = array, (*y)[] = &array;
    x[0];
    (*y)[0]; // same as above, just with more cumbersome syntax
    

    Typically, if a function needs to return an array of int, you either return the pointer or pass the pointer in. One of the following:

    int *func(int a, int b); // Allocated by func
    void func(int a, int b, int *array); // Allocated by caller
    void func(int a, int b, int **array); // Allocated by func
    

    The "struct-hack" also works for arrays that have a fixed size:

    struct { int arr[50]; } array_struct;
    struct array_struct func(int a, int b);
    

    But this is not recommended unless the array is small.

    Rationale:

    Arrays are often large, and often have a size not known until runtime. Since parameters and return values are passed using the stack and registers (on all ABIs I know of), and the stack has a fixed size, it is somewhat dangerous to pass such a large object around on the stack. Some ABIs also don't gracefully handle large return values, potentially causing extra copies of return values to be generated.

    The following code can also be dangerous:

    void func(...)
    {
        int arr[BIG_NUMBER]; // potential for stack overflow
        int *ptr = alloca(sizeof(int) * BIG_NUMBER); // potential for stack overflow
    }
    

提交回复
热议问题