Difference between function arguments declared with & and * in C++ - c++

I typed the following example:
#include <iostream>
double f(double* x, double* y)
{
std::cout << "val x: " << *x << "\n";
std::cout << "val y: " << *y << "\n";
return *x * *y;
}
double f2(double &x, double &y)
{
std::cout << "val x: " << x << "\n";
std::cout << "val y: " << y << "\n";
return x * y;
}
int main()
{
double a, b;
a = 2;
b = 3;
std::cout << f(&a, &b) << "\n";
std::cout << f2(a, b) << "\n";
return 0;
}
In the function f I declare x and y as pointers of which I can get the value by using *x. When calling f I need to pass the address of my passed arguments, that is why I pass &a, &b. f2 is the same except the definition is different.
Now my question is: Are they both really the same concerning memory management? Both not making any copy of the passed value but instead passing a reference?
I wonder about f2 because I couldn't read out the address of x in f2 so I know more about x and y in f (there I know address AND value).
Edit: After doing some more research, I found a quite useful topic:
Pointer vs. Reference
There's also a link to google coding guidelines http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Reference_Arguments which is quite useful I feel (as I understood now, it's a form of subject taste) to make more clear

f2 is taking it's arguments by reference, which is essentially an alias for the arguments you pass. The difference between pointer and reference is that a reference cannot be NULL. With the f you need to pass the address (using & operator) of the parameters you're passing to the pointer, where when you pass by reference you just pass the parameters and the alias is created.
Passing by const reference (const double& ref) is preferred when you are not going to change the arguments inside the function, and when you are going to change them, use non-const reference.
Pointers are mostly used when you need to be able to pass NULL to your parameters, obviously you'd need to check then inside your function if the pointer was not NULL before using it.

This is just syntactic sugar to avoid having to use * everytime you reference the argument. You still can use & to have the address of x in f2.

In my head, parameters of functions are always passed by value. Passing an int is easy to imagine, passing a double is just bigger and passing a struct or class could be very big indeed.
But passing a pointer to something, well, you're just passing an address by value. (A pointer is often a convenient size for the CPU just like an int.)
A reference is very similar, and certainly I think of a reference as a pointer, but with syntactic sugar to make it look like the object its referring to has been passed by value.
You can also think of a reference as a const pointer, ie:
int i;
int j;
int* p = &i; // pointer to i
int* const cp = p; // cp points to i, but cp cannot be modified
p = &j; // OK - p is modified to point to j
*cp = 0; // OK - i is overwritten
cp = &j; // ERROR - cp cannot be modified
int& ri = i; // ri refers to i
ri = 1; // i is overwritten
ri = j; // i is overwritten again
// Did you think ri might refer to j?
So, a pointer does double time: It is a value in its own right, but it can also point to another value when you dereference it, eg: *p.
Also, having reference parameters means that you cannot make them refer to anything else during the lifetime of the function because there's no way to express that.
A reference is supposed not to be able to be initialised with null, but consider this:
void foo(int& i);
int* p = 0;
foo(*p);
This means that pointers should be checked before you use them, but references cannot be checked. The implementation of foo() could try to read from or write to i which will cause an access violation.
In the above example the pointer p should have been checked before being used in the call to foo:
if (p) foo(*p);

Another difference that hasn't been mentioned is that you cannot change what a reference refers to. This doesn't make a lot of difference in the function call example shown in the original question.
int X(10), Y(20);
int *pX = X;
int& rY = Y;
*pX = 15; // change value of X
rY = 25; // change value of Y
pX = Y; // pX now points to Y
rY always points to Y and cannot be moved.
References can't be used to index into simple arrays like pointers.

You should have been able to read x address in both functions.
To do so in f2, you must of course prefix x by a & since there, x is a reference to a double, and you want an address.
A worth noticing difference between references and pointers is that the former cannot be NULL. You must pass something (valid) while when providing a pointer, you must specify in the documentation if passing NULL is allowed/well defined.
Another difference is a matter of readability: using references instead of pointers (when possible) makes the code less cluttered with * and ->.

Related

C++: What is a de-reference actually doing?

I am reading through Stroustrup's 4th edition : The C++ Programming Language. I have a python/java background so the first 4 chapters are fine so far.
In Chapter 3 I saw:
complex& operator+=(complex z) { re+=z.re , im+=z.im; return ∗this; }
That began a day long attempt to write this question:
First I figured out that it is returning a reference to the object and not a copy. As I was able to confirm in this question.
And I was able to understand the difference between returning a reference into a reference variable vs. a regular variable from this question
And I did my own trial
class Test {
public:
Test():x{5}{}
int x;
void setX(int a) {x = a;}
Test& operator+=(Test z) {x+=z.x; return *this;}
// the keyword this is a pointer
Test* getTest() {return this;}
// but I can return the reference by *this
Test& getTest1() {return *this;}
// or I can return a copy
Test getTest2() {return *this;}
};
That lead me to question why it is called de-reference, so I did this trial
int x = 8;
int* p = &x;
int y = *p;
int& z = *p;
x++; // let's have some fun
std::cout << y << std::endl;
std::cout << z << std::endl;
As expected y = 8 and z = 9, so how did the de-reference return the address in one case, and the value in the other? More importantly how is C++ making that distinction?
It's exactly like in your Test class functions.
int y = *p;
int& z = *p;
y is a copy of what p points to.
z is a reference to (not an address) what p points to. So changing z changes *p and vice-versa. But changing y has no effect on *p.
As expected y = 8 and z = 9, so how did the de-reference return the address in one case, and the value in the other? More importantly how is C++ making that distinction?
The de-reference returned the actual thing referenced in both cases. So there is no distinction for C++ to make. The difference is in what was done with the result of the dereference.
If you do int j = <something>; then the result of the something is used to initialize j. Since j is an integer, the <something> must be an integer value.
If you do int &j = <something>; then the result of the something is still used to initialize j. But now, j is a reference to an integer, and the <something> must be an integer, not just an integer value.
So, what *this does is the same in both cases. How you use a value doesn't affect how that value is computed. But how you use it does affect what happens when you use it. And these two pieces of code use the dereferenced object differently. In one case, its value is taken. In the other case, a reference is bound to it.
It's possible to consider a pointer int* p as pointing to an address where data of type int resides. When you de-reference this, the system retrieves the value at that memory address (the address is the actual value of p itself). In the case of int y = *p; you put a copy of that int value on the stack as the locator value y.
On the other hand, de-referencing on the left-hand side in *p = 13; means you are replacing the int value *p stored at the memory address denoted by the value of p with the right-hand-side value 13.
The reference lvalue int& z in int& z = *p; is not a copy of the int value pointed to by p but rather a left-hand side reference to whatever is at the particular memory address returned by *p (i.e. the actual value held by p itself).
This doesn't mean much difference in your contrived case, but e.g. given a Foo class with a Foo::incrementCount() value,
Foo* p = new Foo();
p->incrementCount();
Foo& ref = *p;
ref.incrementCount();
The same method for the same instance will be called twice. In contrast, Foo foo = *p will actually copy the entire Foo instance, creating a separate copy on the stack. Thus, calling foo.incrementValue() won't affect the separate object still pointed to by p.

References and Pointers both store addresses?

Does int* var and &var both store addresses, the only difference is that you have to deference int* to get the value back but don't references already do that? Having trouble understanding these thoroughly.
And why is that when you have a function that accepts int* into the parameters you can pass values in by &
They are different ways of expressing what probably eventually boils down to the same thing. They are both constructs that have been invented by the language's designers, meaning your compiler's authors must implement them in whatever manner they see fit for the underlying machine.
However, just because they may represent the same thing on the machine doesn't mean that they are equivalent. Pointers allow the concept of pointing-to-nothing-ness (NULL pointers) and also allow one to perform mathematic operations to obtain a portion of memory indexed off of a starting position... like so:
int *x = new int [10];
*(x+2) = 5; //set the 3rd element of the array pointed to by 'x' to 5
is perfectly sensible.
References have no notions of such things, i.e. one can do
int *x = new int[10];
*(x+2) = 5;
int &y = *(x+2);
but not
int *x = new int[10];
*(x+2) = 5;
int &y = *(x+2);
y = y + 5;//this just changes the value of x[2]
which means it's more difficult to write off the end of a struct because of bad pointer math, so they are safer provided they've been initialized to something that makes sense (i.e. not returned from a function where they are declared on the stack or to an array element that doesn't exist)
int &dontdoit() {
//don't do this!
int x = 7;
int &y = x;
return y;
}
I think this is perfectly legal and safe in that you won't be corrupting memory, but it's not recommended as you're mixing idioms and you have no way to free the resulting allocated memory:
int &dontdothiseither() {
int *x = new int;
int &y = *x;
return y;
}
Also, you can set a pointer as many times as you like but not a reference.
int x[2];
int *y = x;//works
y = y+1; //works, now points to x[1];
int &z = x[0]; //works
z = x[1];//nope! This just sets x[0] to be the value in x[1]
int* is a variable whose value is the address of some int. &var is not a reference. The unary & operator simply returns the address of var. That's why if you have a function that takes a parameter of type int*, you use &var at the calling site.
It's a bit confusing since C++ uses & to mean both "address of" and "reference", but the context is what makes them different.
int a = 5;
int& ref = a; // Now ref and a both mean the same thing.
vs
int b = 6;
int* ptr = &b; // Now ptr POINTS to b, but they are not the same thing.
no no no!
references become nickname of that variable, where as pointers store variable's address.
suppose we have a variable of type int :
int x;
int &y=x; // now y is an other name of x
int *p=&x; // here p is a pointer, which points to x
Yes, pointers and references both store addresses, and are compiled to exactly the same code. The only major difference though is that references cannot be null, whereas pointers can - hence the well-known "null-pointer". Obviously they are accessed in different ways by the programmer: using -> and . respectively but that is really of no significance.
You can view a reference as a new "name" for a variable, while a pointer is a variable storing an address.
In practice, a reference might be implemented with pointers (so, it might store an address), but you do not need to worry about that.
For example:
int i;
int *pointer = &i; //Holds address of i
int& reference = i; //reference is a new name of i
int *pointer2 = &reference; //pointer2 holds the address of i
Yes, internally passing a pointer to T and passing a reference to T are the same in any known sensible implementation, references being semantic sugar for pointers which are always dereferenced and cannot be NULL.
Implementations are not obligated to make sense though.
After optimization, that is even sure for references used inside a function.
Still, they lead to different method / object signatures.
Also, they have different semantic load for the reader.
The & character is used for many different purposes in C++. Two of them look like they might be similar, but they're not.
The first is to create a reference:
int a = 1;
int &b = a;
Now a and b both refer to the same variable, and a change made to one will be reflected in the other.
The other usage is to create a pointer:
int *p = &a;
This has nothing to do with references at all. The & is being used as an address-of operator, taking the variable and creating a pointer that points to it.

C++ passage by pointer and modification of values

until there I trusted that method like
bool solverMethod::buildSimplex(double** simplex_ , double* funcEvals_, double* initPt_)
{
// things
}
would change values for simplex, funcEvals_, initPt_ in the method where it is called (passage by pointer). Am I wrong? How to put it then?
thanks and regards and apologizes for simple question.
This is maybe not as much an answer as it is a general explanation of pointers, references and reference semantics.
A function is said to have reference semantics if it can change the argument objects that are passed to it. For example, the following swap function has reference semantics if it exchanges the values:
int x = 4;
int y = 8;
swap(x, y);
assert(x == 8 && y == 4);
The question is how you implement reference semantics. C++ has a native reference type that makes this very natural:
void swap(int & a, int & b) { int t = a; a = b; b = t; }
By contrast, C does not have such a native feature, and every object in C is passed by value. However, C has a different feature that can be used to implement reference semantics, namely pointers: For every type T, there is a related type T*, a value of which can be obtained by taking the address-of an object of type T: int x; int * p = &x;. Now you can pass those pointer objects around by value and use them to access the original object to which they point. Notice that we are passing the pointers by value!
void c_swap(int * p, int * q) { int t = *p; *p = *q; *q = t; }
We have to call the function differently: swap(&x, &y). Thus in C you can always tell whether an argument is being modified, because the only way to do this is by passing its address to a function. In C++, you have to know the actual function signature to know whether reference or value semantics are in place.

Does int * & has any real sense?

I'm looking few exercise from university about C++ and I found out this exercise:
#include <iostream>
using namespace std;
int& f(int*&);
int& f(int*& x) {
*x = 5;
return *x;
}
int main() {
int y = 1, x;
int* z = &y;
x= f(z);
cout << y << " " << x <<endl;
}
I was wondering: does <any type>*& has any real sense? Isn't f(int*& x) the same as f(int x)? Aren't you passing the pointer to the L-value of the variable?
f(int*& x) is not the same as f(int x). In the first case x is a reference to an integer pointer whereas in the second case x is just an integer.
Lets start from the basics:
When you write f(int &x) means that x is a reference to an integer and you can change the value of x in the function and the change will be reflected in the calling function.
Similarly, when you write f(int*& x), it means that x is reference to an integer pointer and when you change the address that x points to, the change will also be reflected in the calling function.
With int* &x you are passing the same pointer(by reference). Otherwise with only int* x you are passing a copy of the pointer and then you can't change the original one in the function. &x makes x an alias of the original parameter.
It's a reference to a pointer to an int. The function is then able to change the pointer if it wants to. In your example it doesn't make sense, but it obviously does have a use.
nope: *& here doesn't mean "dereference addressof". It means: "pass a pointer byref".
int& f(int*& x) {
*x = 5; // note: changes the pointee, not the pointer
return *x;
}
In this example, you don't gain anything by passing the pointer by reference, since you're not changing the pointer. Passing a pointer by reference is only needed when you intend to change the pointer:
void f(int*& x) {
x = new int(42); // note: changes the pointer
}
I think you are confusing sybols when they are used as operators or declarators.
If you use * when declaring variable, that means veriable is a pointer. When you use * as operator, that is dereference operator.
int *& name
simply means you are taking pointer to int by reference. **The * and & do not cancel each other out.**
If you had line of code like this:
var = *& var2;
then yes, it would be same as:
var = var2;

Clarification of References in C++

So I am attempting to learn C++ and I have come across something that puzzles me slightly. I have the code,
int x = 0;
int &y = x;
cout << &x<< " " << x << " " << &y << " " <<y<< endl;
This compiles fine and results in:
0 003AFA08 0 003AFA08
What I have trouble understanding why the conversion of x, an int, to &y, a reference, doesn't result in an error. At first I thought it was some sort of conversion however,
int &y = &x;
results in an error.
Can anyone explain why this works in this way? Thanks in advance.
int& is not an address. It is a reference.
int& y = x; declares y as a reference to x. It effectively means that y becomes another name for x. Any time you use y, it is as if you had said x.
Pointers (not references) are used to store addresses. int& y = &x; is not valid because &x is the address of x (it's an int*). y is a reference to an int, not a reference to an int*.
It isn't a conversion. When you have a variable type of T & where T is some random type, you are basically saying "I'm declaring a name which is an alias for another name or possibly an anonymous value.". It's more like a typedef than a pointer.
References happen to often be implemented as addresses, but that isn't a good model for thinking about what they are.
In your example that you're puzzled by:
int * const &y = &x;
would work just fine. Then y becomes an alias for the temporary result of taking the address of x. Notice that it is a reference to a pointer. It has to be a reference to a constant pointer because it is a reference to a temporary value.