# I need an explanation about references

This is a discussion on I need an explanation about references within the C++ Programming forums, part of the General Programming Boards category; Hello, i know some of the basic stuff about references...but what's going on here: Code: #include<iostream> using namespace std; void ...

1. ## I need an explanation about references

Hello, i know some of the basic stuff about references...but what's going on here:

Code:
#include<iostream>

using namespace std;

void foo(int& aa,int& bb){

aa=0;
bb=0;

}

int main(){

int a(1);
int b(1);

foo(a,b);

cout<<a<<" "<<b<<endl;

return 0;
}
When i call foo function, does it push its arguments onto the stack? What is the layout of a stack frame? What is the difference between pass by reference and pass by a pointer?

I read somewhere that there are no references to references, but i compile this:

Code:
#include<iostream>

using namespace std;

int main(){

int a(0);
int& b=a;
int& c=b;

return 0;
}

Thanks

2. Originally Posted by 4c0
When i call foo function, does it push its arguments onto the stack? What is the layout of a stack frame? What is the difference between pass by reference and pass by a pointer?
You could look at the stack frame:
Code:
#0 00000000	foo(aa=@0x28fefc: 1, bb=@0x28fef8: 1) (C:\Users\Josh2\Documents\bar\bar2.cpp:7)
#1 004013B0	main() (C:\Users\Josh2\Documents\bar\bar2.cpp:18)
Clearly something is being passed in as an argument. It is functionally equivalent to a pointer, if that isn't confusing. References are a way to pass an object to a function without worrying about copying it or NULL references from a pointer.

I read somewhere that there are no references to references, but i compile this:
That is reference assignment, which you can do. It creates two references to the same object. In the code you posted, you have the reference to int b, and reference to int c both referencing the integer a. It's not exactly the same as a pointer to a pointer. In that sort of situation, you have a pointer value, which at that memory location also stores a pointer value.

3. Originally Posted by 4c0
When i call foo function, does it push its arguments onto the stack? What is the layout of a stack frame?
Does it matter? All you need to know is what it does.
These things are not really defined in the standard and will vary between different platforms and compilers, so there is no single answer to this question. So again, what does it matter?

What is the difference between pass by reference and pass by a pointer?
A reference is simple an alias (a different name) for a variable you pass in. So you are referring to a variable you passed in, but you've named it differently.
A pointer passes the address of a variable, which you can then use to write and read to/from the address specified in the variable (and obviously change the address (value) stored inside the variable, too).

I read somewhere that there are no references to references...
References to references are illegal. Consider this:
Code:
int main()
{
int x;
int&&& z = x;
}
This gives a compile error:
error C2529: 'z' : reference to reference is illegal

As to why there are 3 &, it's because && is also a type of reference (r-value reference); thus, it's legal, while &&& is not.

4. These things are not really defined in the standard and will vary between different platforms and compilers, so there is no single answer to this question. So again, what does it matter?

5. Originally Posted by 4c0
I read somewhere that there are no references to references, but i compile this:
Code:
#include<iostream>

using namespace std;

int main(){

int a(0);
int& b=a;
int& c=b;

return 0;
}
That is not an example of references to references. b and c are both alternative names for (or references to) the variable a. b is a reference to a. c is a reference to the same variable as b (i.e. to a). It is not a reference to a reference.

Originally Posted by Elysia
As to why there are 3 &, it's because && is also a type of reference (r-value reference); thus, it's legal, while &&& is not.
r-value references are only valid in C++-11. Not in earlier versions of C++.

6. Originally Posted by whiteflags
Unless you are optimizing for a system, that's not necessary.

7. You're wrong.

8. *shrug*
I can be wrong. It doesn't matter to me.
But I'm also not going to believe it has any practical purpose unless you are dealing with such a low level unless someone can prove otherwise.

9. I agree with elysia here. I have found, in my own experience, that knowing the details of the implementation can be more of a hindrance than a help. with such knowledge, a person is more likely to make assumptions, which may not hold true on all systems.

it is much better to write standard-compliant code, and have faith that the developers of the platform/compiler know what they're doing.

if the intent is to write code at a low level, such as an OS kernel or a compiler, it may be necessary to understand the implementation of such things, but in the overwhelming majority of other cases, the compiler knows better than you do.

10. So does references take up memory space when i use them as with parameter passing? It depends on the implementation?

11. It most certainly depends on the implementation.

Section 8.3.2 paragraph 4 of the most recent draft C++ language standard:
4 It is unspecified whether or not a reference requires storage (3.7).
This was true in earlier C++ standards and it is that way now.

12. I only recently started learning C++ however from what I know up till now I tend to look at C++ "passed-by-reference" as basically const pointers. They both accomplish the same thing but one requires less code. Doesn't mean pointers cannot accomplish the same thing
Code:
  1 #include<iostream>
2 using namespace std;
3
4 void foo(int& a,int& b){
5
6     a=0;
7     b=0;
8 }
9
10 void foo_ptrs(int* a, int * b){
11
12    *a = 0;
13    *b = 0;
14 }
15
16 int main(void){
17
18    int a = 1, b = 1;
19
20    foo(a,b);
21    cout << "using foo() a = " << a <<" b ="<< b <<endl;
22
23    foo_ptrs(&a,&b);
24    cout <<"using foo_ptrs() a = " << a << " b = " << b << endl;
25
26    return 0;
27 }
A reference does the same thing as a pointer in that it allows you to access the address of a variable and change it from within function scope. However
It is supposed to be "safter" in that you cannot change the value it is pointing to. Whereas in C you can. C++ folks will argue its merits but for now
I simply use it and see it as
Code:
const int* ptr
or
Code:
int const *ptr
.

Always opened to hear more from those who've been working with it longer though. I'm still a C fan boy at heart

13. Originally Posted by hex_dump
It is supposed to be "safter" in that you cannot change the value it is pointing to.
And it is illegal to obtain a "null reference", whereas it is legal to pass a null pointer, hence the function must check for it or just declare the behaviour to be undefined.

Originally Posted by hex_dump
I simply use it and see it as
Code:
const int* ptr
or
Code:
int const *ptr
I think you mean:
Code:
int * const ptr

14. Choosing to use a pointer in C++ usually means that you need something a reference doesn't support. References can't be NULL, and references can't be reassigned. This makes references a poor choice in certain situations. If a parameter is optional, a pointer might be preferred, since you can pass NULL. It's the same in the opposite way. If a parameter is required, using a reference would enforce the requirement. The compiler would have to check all of the arguments, and a reference can only be created with a live object.

Pointers are also used to manage dynamic storage, but they are usually encapsulated in objects called "smart pointers," so you can reference those instead.

That said, hanging references isn't impossible in C++:
Code:
int *p = new int;
int& pref = *p;
delete p;
It does tend to be rather silly if you manage it though. It's exactly like using variables out of scope, and conventional C++ tends to follow the rules of scope, as a foundation for memory management, for instance. That's the real reason why C++ users say it is safe.

In order to conceptually understand references, you might initially think that they are pointers. I think this is appropriate. But just as quickly, you will want to discard that idea, as they are separate types with different capabilities. Once you understand the different capabilities, your initial thoughts are actually quite wrong.

15. I think you mean:
Code:
int * const ptr
Code:
#include <stdio.h>

int main(void){

int a = 5, b=11;
const int* ptr1 = &a;
int const *ptr2 = &b;

printf("ptr1=%d ptr2=%d \n", *ptr1, ++*ptr2);
return 0;
}
I get this
Code:
cc -Wall some.c -o some
some.c: In function ‘main’:
some.c:10:4: error: increment of read-only location ‘*ptr2’
meaning the compiler accepted it. My brain shuts down sometimes at night so...

Page 1 of 2 12 Last