Why can't we pass arrays to function by value?

2019-01-07 10:23发布

问题:

Apparently, we can pass complex class instances to functions, but why can't we pass arrays to functions?

回答1:

The origin is historical. The problem is that the rule "arrays decay into pointers, when passed to a function" is simple.

Copying arrays would be kind of complicated and not very clear, since the behavior would change for different parameters and different function declarations.

Note that you can still do an indirect pass by value:

struct A { int arr[2]; };
void func(struct A);


回答2:

Here's another perspective: There isn't a single type "array" in C. Rather, T[N] is a a different type for every N. So T[1], T[2], etc., are all different types.

In C there's no function overloading, and so the only sensible thing you could have allowed would be a function that takes (or returns) a single type of array:

void foo(int a[3]);  // hypothetical

Presumably, that was just considered far less useful than the actual decision to make all arrays decay into a pointer to the first element and require the user to communicate the size by other means. After all, the above could be rewritten as:

void foo(int * a)
{
  static const unsigned int N = 3;
  /* ... */
}

So there's no loss of expressive power, but a huge gain in generality.

Note that this isn't any different in C++, but template-driven code generation allows you to write a templated function foo(T (&a)[N]), where N is deduced for you -- but this just means that you can create a whole family of distinct, different functions, one for each value of N.

As an extreme case, imagine that you would need two functions print6(const char[6]) and print12(const char[12]) to say print6("Hello") and print12("Hello World") if you didn't want to decay arrays to pointers, or otherwise you'd have to add an explicit conversion, print_p((const char*)"Hello World").



回答3:

Answering a very old question, as Question is market with C++ just adding for completion purposes, we can use std::array and pass arrays to functions by value or by reference which gives protection against accessing out of bound indexes:

below is sample:

#include <iostream>
#include <array>

//pass array by reference
template<size_t N>
void fill_array(std::array<int, N>& arr){
    for(int idx = 0; idx < arr.size(); ++idx)
        arr[idx] = idx*idx;
}

//pass array by value
template<size_t N>
void print_array(std::array<int, N> arr){
    for(int idx = 0; idx < arr.size(); ++idx)
        std::cout << arr[idx] << std::endl;
}

int main()
{
    std::array<int, 5> arr;
    fill_array(arr);
    print_array(arr);
    //use different size
    std::array<int, 10> arr2;
    fill_array(arr2);
    print_array(arr2);
}


回答4:

The reason you can't pass an array by value is because there is no specific way to track an array's size such that the function invocation logic would know how much memory to allocate and what to copy. You can pass a class instance because classes have constructors. Arrays do not.



回答5:

The equivalent of that would be to first make a copy of the array and then pass it to the function (which can be highly inefficient for large arrays).

Other than that I would say it's for historical reasons, i.e. one could not pass arrays by value in C.

My guess is that the reasoning behind NOT introducing passing arrays by value in C++ was that objects were thought to be moderately sized compared to arrays.

As pointed out by delnan, when using std::vector you can actually pass array-like objects to functions by value.



回答6:

You are passing by value: the value of the pointer to the array. Remember that using square bracket notation in C is simply shorthand for de-referencing a pointer. ptr[2] means *(ptr+2).

Dropping the brackets gets you a pointer to the array, which can be passed by value to a function:

int x[2] = {1, 2};
int result;
result = DoSomething(x);

See the list of types in the ANSI C spec. Arrays are not primitive types, but constructed from a combination of pointers and operators. (It won't let me put another link, but the construction is described under "Array type derivation".)



回答7:

Summery:

  1. Passing the Address of the array's first element &a = a = &(a[0])
  2. New Pointer (new pointer, new address, 4 bytes, in the memory)
  3. Points to the same memory location, in different type.

Example 1:

void by_value(bool* arr) // pointer_value passed by value
{
    arr[1] = true;
    arr = NULL; // temporary pointer that points to original array
}

int main()
{
    bool a[3] = {};
    cout << a[1] << endl; // 0
    by_value(a);
    cout << a[1] << endl; // 1 !!! 
}

Addresses:

[main] 
     a = 0046FB18 // **Original**
     &a = 0046FB18 // **Original**
[func]
     arr = 0046FB18 // **Original**
     &arr = 0046FA44 // TempPTR
[func]
     arr = NULL
     &arr = 0046FA44 // TempPTR

Example 2:

void by_value(bool* arr) 
{
    cout << &arr << arr; // &arr != arr
}

int main()
{
    bool a[3] = {};
    cout << &a << a; // &a == a == &a[0]
    by_value(arr);
}

Addresses

Prints: 
[main] 0046FB18 = 0046FB18
[func] 0046FA44 != 0046FB18

Please Note:

  1. &(required-lvalue): lvalue -to-> rvalue
  2. Array Decay: new pointer (temporary) points to (by value) array address

readmore:

Rvalue

Array Decay



回答8:

It was done that way in order to preserve syntactical and semantic compatibility with B language, in which arrays were implemented as physical pointers.

A direct answer to this question is given in Dennis Ritchie's "The Development of the C Language", see the "Critique" section. It says

For example, the empty square brackets in the function declaration

int f(a) int a[]; { ... }

are a living fossil, a remnant of NB's way of declaring a pointer; a is, in this special case only, interpreted in C as a pointer. The notation survived in part for the sake of compatibility, in part under the rationalization that it would allow programmers to communicate to their readers an intent to pass f a pointer generated from an array, rather than a reference to a single integer. Unfortunately, it serves as much to confuse the learner as to alert the reader.

This should be taken in the context of the previous part of the article, especially "Embryonic C", which explains how introduction of struct types in C resulted in rejection of B- and BCPL-style approach to implementing arrays (i.e. as ordinary pointers). C switched to non-pointer array implementation, keeping that legacy B-style semantics in function parameter lists only.

So, the current variant of array parameter behavior is a result of a compromise: one the one hand, we had to have copyable arrays in structs, on the other hand, we wanted to preserve semantic compatibility with functions written in B, where arrays are always passed "by pointer".



回答9:

actually, a pointer to the array is passed by value, using that pointer inside the called function will give you the feeling that the array is passed by reference which is wrong. try changing the value in the array pointer to point to another array in your function and you will find that the original array was not affected which means that the array is not passed by reference.