Pointer and Reference⚓︎
This material is focusing on describing the difference when passing parameters
Definition difference⚓︎
Definition diff?
- A
pointer
is a variable, except that the variable stores an address that points to a storage unit in memory. - A
reference
is essentially the same thing as the original variable, just an alias for the original variable
- The value of the
pointer
is changeable after initialization - A
reference
can only be initialized once in a definition. Then it cannot be changed.
- Const pointer:
int* const a
- No const reference: no
int& const a
- We can have a const referred value:
const int& a
Pointer
's value can beNULL
.Reference
's value cannot beNULL
. References must be initialized when they are defined
Pointer
: the size of pointer.Reference
': the size of referred variable.
Pointer
is not type safeReference
is type safe since it has type checking
Pointer
can have several layers such asint** p
Reference
can not have several layer such asint&& p
Parameter passing diff⚓︎
Pass by value⚓︎
What is pass by value?
-
When calling function passes parameter in called function,
stack
will open up a new space for storing a memory copy of the value ofactual parameter
from the calling function. -
Any operation in the called function on a formal parameter is performed as a local variable since you are using the copied value. It does not affect the value of the
actual parameter
of the main calling function.
#include<iostream>
using namespace std;
void Rfun(int p) // Called function: copy the value from "a"
{
cout<<"local p's address:"<<&p<<endl; //0x6ffdf0
cout<<"local p's value:"<<p<<endl; //16
p=0Xff;
}
int main()
{
int a=0x10;
cout<<"a's address:"<<&a<<endl; //0x6ffe1c
cout<<"a's value:"<<a<<endl; //16
Rfun(a); // Calling function: passing a's value
cout<<"a's now address:"<<a<<endl; //16
}
>>>
a's address:0x7ffd8a777d04
a's value:16
local p's address:0x7ffd8a777cec
local p's value:16
a's now address:16
Pointer passing⚓︎
Pointer passing
Pointer parameter passing
is essentially Pass by value
. It passes an address as value.
#include<iostream>
using namespace std;
void Pfun(int* p) // Called function: copy the address value from "a"
{
//new address for storing address value passed by "a"
cout<<"local p's address:"<<&p<<endl; //0x6ffdf0
cout<<"local p's value:"<<p<<endl; //0x6ffe1c
//test if p points to a's value
cout<<"local p's pointing value:"<<*p<<endl; //16
*p=0Xff;
//test if p will affect "a"'s address value
p++;
cout<<"local p's updated value:"<<p<<endl; //0x6ffe1d
}
int main()
{
int a=0x10;
cout<<"a's address:"<<&a<<endl; //0X6ffe1c
cout<<"a's value:"<<a<<endl; //16
Pfun(&a); // Calling function: passing a's address
cout<<"a's now address:"<<&a<<endl; //0X6ffe1c
cout<<"a's now value:"<<a<<endl; //255
}
<<<
a's address:0x7ffd233a9854
a's value:16
local p's address:0x7ffd233a9838
local p's value:0x7ffd233a9854
local p's pointing value:16
local p's updated value:0x7ffd233a9858
a's now address:0x7ffd233a9854
a's now value:255
Reference passing⚓︎
Reference passing
-
When calling function passes parameter in called function,
stack
will open up a new space for storing a memory copy of the address ofactual parameter
from the calling function. -
Any operation in the called function on a formal parameter will affect the value of the
actual parameter
since you are tracking and editing the content in the same address.
#include<iostream>
using namespace std;
void Rfun(int &p)
{
cout<<"local p's address:"<<&p<<endl; //0x6ffdf0
cout<<"local p's value:"<<p<<endl; //16
p=0Xff;
}
int main()
{
int a=0x10;
cout<<"a's address:"<<&a<<endl; //0x6ffe1c
cout<<"a's value:"<<a<<endl; //16
Rfun(a);
cout<<"a's now address:"<<a<<endl; //255
}
>>>
a's address:0x7ffebbcabd44
a's value:16
local p's address:0x7ffebbcabd44
local p's value:16
a's now address:255