Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between passing an array by value and reference in C++

Tags:

c++

Is there any difference between the below two functions test1 and test2

static int const MAXL = 3;

void test1(int t[MAXL])
{
    for (int i = 0; i < MAXL; ++i)
        t[i] = 10;   
}

void test2(int (&t)[MAXL])
{
   for (int i = 0; i < MAXL; ++i)
       t[i] = 10;    
}

With my testing in MSVC2008, both functions modifies the input array values. It seems both the functions are same in their functionality.

Can anyone give a case that need a reference to array in a function parameter?

like image 965
FaisalM Avatar asked Jul 10 '13 03:07

FaisalM


People also ask

Is array pass by value or reference in C?

Passing arrays to functions in C/C++ are passed by reference. Even though we do not create a reference variable, the compiler passes the pointer to the array, making the original array available for the called function's use. Thus, if the function modifies the array, it will be reflected back to the original array.

What is the difference between by reference and by value?

In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument. In the case of Call by Reference, when we pass the parameter's location reference/address, it copies and assigns them to the function's local argument.

Is array pass by value or reference?

Like all Java objects, arrays are passed by value ... but the value is the reference to the array. Real passing by reference involves passing the address of a variable so that the variable can be updated.

Is it better to pass by reference or value?

Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program. In pass by reference (also called pass by address), a copy of the address of the actual parameter is stored.


1 Answers

The first one decays to a pointer to the first element in the array, the second is an actual reference to the array.

They're different in the same way that pointers and references are generally different.

Specifically, in the case of arrays, a reference to the array is useful because you retain the ability to determine the size of the array. This frees you from having to pass the size/length of the array as a separate parameter as you would in a C API.

One way of implementing this that I think is particularly slick involves templates. Using a template parameter, you can get the compiler to automatically deduce the size of the array. For example:

void ProcessArray(int* pArray, std::size length)
{
    for (std::size_t i = 0; i < length; ++i)
    {
        // do something with each element in array      
    }
}

template <std::size_t N>
void ProcessArray(int (&array)[N])
{
    ProcessArray(array, N);  // (dispatch to non-template function)
}
like image 59
Cody Gray Avatar answered Oct 20 '22 00:10

Cody Gray