array - copied or not copied

Is the array below copied or not copied when called? Told to use & in the type of the function so it isn't copied, but without the & it still changes its argument outside of the function, not really consistent with copying the array in the function.

code snippet:
1
2
3
4
5
const int& getElement(const int *array, int index)
{
    return array[index];
}


Also how does the reference operator & effect index? is it copied?
The first parameter here is a pointer to the first element of the array. The array isn't copied.

If you pass that pointer by reference, the array still isn't copied. You're passing a pointer to the first element in the array - you're not passing the array.
Last edited on
Exactly. I meant to say ptr to first element is passed. So why use an & in the function type?

Is the int index copied?

Thx for your time.
Is the array below copied or not copied when called? Told to use & in the type of the function so it isn't copied, but without the & it still changes its argument outside of the function, not really consistent with copying the array in the function.


Not copied. index is a copy of the argument you fed to the function. I'm not sure what you mean when you say "it still changes its argument outside of the function" since this function modifies nothing at all, and the reference doesn't even apply to the parameters/arguments of the function.

If there were a reference involved in this function, I expect it would look more like this:

1
2
3
4
void getElement(const int* array, int index, int& value) 
{
    value = array[index];
}


I'm slightly confused by your question. Does this help? Are you talking about copying the array elements? If so, it depends:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

const int& getElement(const int* arr, int index) {
    return arr[index];
}

int main() {
    int arr[5] = { 0, 1, 2, 3, 4 };
    
    // all the following have the same address.
    std::cout << arr[0] << ": " << &arr[0] << std::endl;
    std::cout << getElement(arr, 0) << ": " << &getElement(arr, 0) << std::endl;

    const int& elem = getElement(arr, 0);
    std::cout << elem << ": " << &elem << std::endl;
    

    // but, this is different:
    int val = getElement(arr, 0);
    std::cout << val << ": " << &val << std::endl;
    
    return 0;
}

http://coliru.stacked-crooked.com/a/dbfe801e4ff54b35
Last edited on
Also how does the reference operator & effect index? is it copied?


http://en.cppreference.com/w/cpp/language/copy_elision

When one returns a value from a function, there are a bunch of things the compiler tries to do implicitly:

* Copy Elision NRVO Named Return Value Optimisation
* Copy Elision RVO Return Value Optimisation
* Move Semantics - uses std::move implicitly if copy elision isn't feasible
* Copy - if everything else isn't feasible

So, one might not need to return by reference - it is already efficient because of the copy elision or move semantics.

As cire points out, you may not need to return anything if using references for the function parameters - you have already changed the values of the variables.

Also be wary of returning a reference to something which goes out of scope at the end of the function.

Passing by reference is a term that includes both pointers and C++ references. In C, there are no references: passing by pointer is still called pass by reference, as opposed to pass by value.

So in summary, pass by reference if:

There is a need to change the value in the outer scope;
The object is an STL container or a class, struct - anything that might potentially have some large size.

Make the parameters const wherever it is valid to do so.

If not changing a value in the outer scope, don't bother to pass by reference for basic built-in types (int or double say) - it's not worth it.

My question boils down to: why do I need & in the actual function type (plz see fx below): what does it mean, and should I include it?

The code below just shows that I understand how with & you can move data around without copying or relying on a single fx return. It's pretty neat they way that happens.

All the confusion over array copying came from what I thought was a reputable source ::let's just say the correct notion has been reinstilled here about arrays and passing the pointer to the first element of the array.

FX with & in type

const int*--->&getElement(const int *array, int &index, int &value)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
//lets move stuff around!
const int &getElement(const int *array, int &index, int &value)
{
	value = array[index];
	std::cout<<"Value get element :"<<value<<std::endl;
}

void print_array(int * array)
{
	for (int i = 0; i<5; ++i)
	{
		std::cout<<array[i]<<"  ";
	}
	std::cout<<std::endl;
}

void change_array_in_fx(int * array)
{
	array[0] = -5;
	array[4] = 9900;
}

int main()
{

	int * array = new int[5] {5,3,44,99,10};
	print_array(&array[0]);
	std::cout<<"In main before change: ";
	std::cout<<array[0]<<"  ";
	std::cout<<array[4]<<std::endl;
	change_array_in_fx(&array[0]);
	std::cout<<"In main after change: ";
	std::cout<<array[0]<<"  ";
	std::cout<<array[4]<<"  "<<std::endl;
	std::cout<<"Find indexed value. Input index number: ";
	int index(0);
	int value = 0;//nt value = 4;
	//int &value = value;
	std::cin>>index;
	getElement(&array[0],index, value);
	std::cout<<"Main index: "<<index<<std::endl;

	return 0;


Last edited on
My question boils down to: why do I need & in the actual function type (plz see fx below): what does it mean, and should I include it?
...
FX with & in type

const int*--->&getElement(const int *array, int &index, int &value)


What do you think the "actual function type" is? The & your makeshift arrow points to is part of the return type.

No, you don't need to be returning a reference here, although it may make sense in generic template code that may be dealing with heavier types.

By the way, your last version of the function promises to return something and does not. Don't do that.
My question boils down to: why do I need & in the actual function type (plz see fx below): what does it mean, and should I include it?


As per my last post, you probably don't need it.

You should return a reference for things like overloading stream operators (operator<< and operator>>):

http://en.cppreference.com/w/cpp/language/operators

Not sure if there are other reasons for wanting to return a reference from a function?

One aspect of this (I guess) is that references (1985) were around long before move semantics (needed rvalue references) were part of the C++11 standard. Not sure about copy elision (C++03?), but the idea of RVO by Walter Bright in 1991. I imagine that early C++ code would have had a void function with references as parameters for the things that were being modified, and one could still (and would normally?) do that today.

http://en.cppreference.com/w/cpp/language/history
https://en.wikipedia.org/wiki/Return_value_optimization
Thank you everyone for helping me.
Last edited on
Topic archived. No new replies allowed.