Beruflich Dokumente
Kultur Dokumente
Programming
Topic 6 Reference, Pointer, and Passing Parameter
OUTLINE MATERIALS
Topic 6 Reference, Pointer, and Passing Parameter
Reference
Pointer
Dangling Pointer
Dynamic Memory Allocation
Pointer this
Passing Parameters
Passing by Value
Passing by Address
Passing by Reference
Passing by Const Reference
Topic 6 Reference, Pointer, and
Passing Parameter
REFERENCE
A reference is an alias or synonym for another variables.
Areference(orconstant reference)variableserves as an
alternative name for an object.
References areanadditionalname toanexistingmemory location
It is declared by using the address operator & appended to the
references type
A reference must be initialized when it is declared.
Contains the address of a variable (like a pointer) but no need to
perform any dereferencing (unlike a pointer)
Ex : int &count;
Means : count is a reference to an int.
REFERENCE (2)
Areferencevariable isdifferentfromapointer
A pointer needNOTbe initialized while defining, but a
reference variable should always refer to some other object
Apointercan be assigned a new value to point at a different
object, but areference variablealways refers to the same
object.
Assigning a reference variable with a new value actually
changes the value of the referred object.
Referenceandconstant referencevariables are commonly
used for parameter passing to a function
They can also be used as local variables or as class data
members
REFERENCE EXAMPLE
# include< iostream >
using nam espace std;
int m ain() {
int n = 33;
int& r = n;// r is a reference for n
cout < < "n = " < < n < < ", r = " < < r < < endl;
n--;
cout < < "n = " < < n < < ", r = " < < r < < endl;
r *= 2;
cout < < "n = " < < n < < ", r = " < < r < < endl;
cin.get();
return 0;
}
CONSTANT REFERENCE
Aconstant reference variablevrefers to an object whose
value cannot be changed throughv.
int m=8;
const int&v=m;
m =16; //valid
v =20; //compilation error
REFERENCING IS THE OPPOSITE OF
DEREFERENCING
p reference n
r dereference p
so r is alias for n
they are different name for the same value.
int m ain() {
int n = 33;
int* p = & n;// p points to n
int& r = *p;// r is a reference for n
cout < < r = < < r < < endl;
}
POINTER
The variable p is called a pointer because its value points to the
location of another value.
The value of a pointer is an address
It is declared by using the operator * appended to the pointers
type
A pointer may not be initialized (refers to NULL) when it is declared.
The operator * is called dereferencing operator
int m ain() {
int n = 33;
int *p = & n;
cout < < *p = < < *p < < endl;
}
THREE TYPES OF MEMORY
In C/C++ programs, three types of memory
are used :
Static memory for global and static variables live
Heap memory is dynamically allocated at
execution time and "managed" memory
accessed using pointers (unnamed variable)
Stack memory for automatic variables and
function parameters
THREE TYPES OF PROGRAM DATA
STATIC DATA: Allocated at compiler time
DYNAMIC DATA: explicitly allocated and de-
allocated during program execution by C++
instructions written by programmer using operators
new and delete
AUTOMATIC DATA: automatically created at
function entry, resides in activation frame of the
function, and is destroyed when returning from
function
DYNAMIC MEMORY ALLOCATION
In C++ dynamic memory allocation is accomplished
using the new and delete operators
new is used to allocate memory during execution
time
returns a pointer to the address where the object is to be
stored
always returns a pointer to the type that follows the new
delete is used to de-allocate memory
SYNTAX FOR NEW AND DELETE
new dataType;
new dataType [intExpression];
delete pointer;
delete [] pointer;
SOLUTION
1. Initialize pointers when they are declared
int x = 7491; // allocates named memory
int* p = &x;
cout << *p;
DANGLING POINTER (2)
2. allocate memory for the pointer explicitly
int* p;
p = new int;
*p = 7491;
4. another way
int* p = new int (7491); // allocates unnamed memory
DANGLING POINTER (3)
Example de-allocated pointer
int* p = new int (7491);
delete p;
*p = 7491; // ERROR
Note: a pointer to constant cannot be deleted
const int* p = new int;
delete p; //ERROR
int m ain() {
int a = 19,b = 47;
cout < < a < < " " < < b < < endl;
sw ap (a,b);
cout < < a < < " " < < b < < endl;
cin.get();
return 0;
}
EXAMPLE: PASSING BY CONST REFERENCE
void calculate (const int & a, const int & b){
a = a+ 1; //error : cannot m odify const object
int c = a + b;
cout < < "c = " < < endl;
}
int m ain(){
int x = 1, y = 2;
calculate (x, y);
cout < < "x = "< < x;
cin.get();
return 0;
}
Q&A
References
Deitel, P., & Deitel, H. (2012). C++ How to Program.8 th
edition. New Jersey: Prentice Hall. Chapter 6 & 8
THANK YOU