0% found this document useful (0 votes)
5 views

Module 07 References

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module 07 References

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Module M07

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outlines
Module M07: Reference & Pointer

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++ Partha Pratim Das

N
const Reference
Parameter

Return-by- Department of Computer Science and Engineering


Reference Indian Institute of Technology, Kharagpur
Pitfalls

I/O Params of a [email protected]


Function

Recommended
Mechanisms All url’s in this module have been accessed in September, 2021 and found to be functional
References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.1


Module Recap

Module M07

• Revisited manifest constants from C

L
Partha Pratim
Das
• Understood const-ness, its use and advantages over manifest constants, and its

E
Objectives &
Outlines interplay with pointers

T
Reference
Pitfalls
• Understood the notion and use of volatile data
• Revisited macros with parameters from C

P
Call-by-Reference
Swap in C
Swap in C++ • Understood inline functions, their advantages over macros, and their limitations

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function

Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.2


Module Objectives

Module M07

• Understand References in C++

L
Partha Pratim
Das
• Compare and contrast References and Pointers

E
Objectives &
Outlines

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function

Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.3


Module Outline

Module M07
1 Reference Variable

L
Partha Pratim
Das
Pitfalls in Reference

E
Objectives &
Outlines 2 Call-by-Reference

T
Reference Simple C Program to swap
Pitfalls
Simple C/C++ Program to swap two numbers

P
Call-by-Reference
Swap in C const Reference Parameter
Swap in C++

N
const Reference
Parameter 3 Return-by-Reference
Return-by- Pitfalls of Return-by Reference
Reference
Pitfalls
4 I/O Parameters of a Function
I/O Params of a
Function

Recommended
5 Recommended Call and Return Mechanisms
Mechanisms

References vs.
6 Difference between Reference and Pointer
Pointers

Module Summary 7 Module Summary


Programming in Modern C++ Partha Pratim Das M07.4
Reference Variable

Module M07

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function Reference Variable
Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.5


Reference

Module M07

• A reference is an alias / synonym for an existing variable

L
Partha Pratim
Das
int i = 15; // i is a variable

E
Objectives &
Outlines int &j = i; // j is a reference to i

T
Reference
Pitfalls
i ← variable

P
Call-by-Reference
Swap in C 15 ← memory content
Swap in C++
← address &i = &j

N
200
const Reference
Parameter
j ← alias or reference
Return-by-
Reference
Pitfalls

I/O Params of a
Function

Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.6


Program 07.01: Behavior of Reference
#include <iostream>
Module M07
using namespace std;

L
Partha Pratim
Das int main() {
int a = 10, &b = a; // b is reference of a

E
Objectives &
Outlines
// a and b have the same memory location

T
Reference cout << "a = " << a << ", b = " << b << ". " << "&a = " << &a << ", &b = " << &b << endl;
Pitfalls

P
Call-by-Reference ++a; // Changing a appears as change in b
Swap in C cout << "a = " << a << ", b = " << b << endl;
Swap in C++

N
const Reference ++b; // Changing b also changes a
Parameter
cout << "a = " << a << ", b = " << b << endl;
Return-by- }
Reference
Pitfalls

I/O Params of a a = 10, b = 10. &a = 002BF944, &b = 002BF944


Function a = 11, b = 11
Recommended a = 12, b = 12
Mechanisms

References vs.
Pointers • a and b have the same memory location and hence the same value
• Changing one changes the other and vice-versa
Module Summary

Programming in Modern C++ Partha Pratim Das M07.7


Pitfalls in Reference

Module M07 Wrong declaration Reason Correct declaration

L
Partha Pratim
Das int& i; no variable (address) to refer to – must be initialized int& i = j;
int& j = 5; no address to refer to as 5 is a constant const int& j = 5;

E
Objectives &
Outlines int& i = j + k; only temporary address (result of j + k) to refer to const int& i = j + k;

T
Reference
Pitfalls #include <iostream>

P
Call-by-Reference using namespace std;
Swap in C
Swap in C++
int main() {

N
const Reference
Parameter int i = 2;
Return-by- int& j = i;
Reference
const int& k = 5; // const tells compiler to allocate a memory with the value 5
Pitfalls
const int& l = j + k; // Similarly for j + k = 7 for l to refer to
I/O Params of a
Function

Recommended
cout << i << ", " << &i << endl; // Prints: 2, 0x61fef8
Mechanisms cout << j << ", " << &j << endl; // Prints: 2, 0x61fef8
References vs. cout << k << ", " << &k << endl; // Prints: 5, 0x61fefc
Pointers cout << l << ", " << &l << endl; // Prints: 7, 0x61ff00
Module Summary }
Programming in Modern C++ Partha Pratim Das M07.8
Call-by-Reference

Module M07

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function Call-by-Reference
Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.9


C++ Program 07.02: Call-by-Reference

Module M07 #include <iostream>


using namespace std;

L
Partha Pratim
Das
void Function_under_param_test( // Function prototype
int&, // Reference parameter

E
Objectives &
Outlines int); // Value parameter

T
Reference
Pitfalls
int main() { int a = 20;
cout << "a = " << a << ", &a = " << &a << endl << endl;

P
Call-by-Reference
Function_under_param_test(a, a); // Function call
Swap in C
}
Swap in C++
void Function_under_param_test(int &b, int c) { // Function definition

N
const Reference
Parameter cout << "b = " << b << ", &b = " << &b << endl << endl;
Return-by-
cout << "c = " << c << ", &c = " << &c << endl << endl;
Reference }
Pitfalls ------- Output -------
a = 20, &a = 0023FA30
I/O Params of a
Function b = 20, &b = 0023FA30 // Address of b is same as a as b is a reference of a
c = 20, &c = 0023F95C // Address different from a as c is a copy of a
Recommended
Mechanisms • Param b is call-by-reference while param c is call-by-value
References vs. • Actual param a and formal param b get the same value in called function
Pointers • Actual param a and formal param c get the same value in called function
Module Summary
• Actual param a and formal param b get the same address in called function
• However, actual param a and formal param c have different addresses in called function
Programming in Modern C++ Partha Pratim Das M07.10
C Program 07.03: Swap in C

Module M07 Call-by-value – wrong Call-by-address – right

L
Partha Pratim
Das
#include <stdio.h> #include <stdio.h>

E
Objectives & void swap(int, int); // Call-by-value void swap(int *, int *); // Call-by-address
Outlines int main() { int a = 10, b = 15; int main() { int a=10, b=15;
printf("a= %d & b= %d to swap\n", a, b); printf("a= %d & b= %d to swap\n", a, b);

T
Reference
Pitfalls
swap(a, b); swap(&a, &b); // Unnatural call
printf("a= %d & b= %d on swap\n", a, b); printf("a= %d & b= %d on swap\n", a, b);

P
Call-by-Reference
} }
Swap in C
void swap(int c, int d) { int t; void swap(int *x, int *y) { int t;
Swap in C++

N
const Reference
t = c; c = d; d = t; t = *x; *x = *y; *y = t;
Parameter } }
Return-by-
Reference
Pitfalls • a= 10 & b= 15 to swap • a= 10 & b= 15 to swap
I/O Params of a
• a= 10 & b= 15 on swap // No swap • a= 15 & b= 10 on swap // Correct swap
Function

Recommended
Mechanisms • Passing values of a=10 & b=15 • Passing Address of a & b
• In callee; c = 10 & d = 15 • In callee x = Addr(a) & y = Addr(b)
References vs.
Pointers
• Swapping the values of c & d • Values at the addresses is swapped
• No change for the values of a & b in caller • Desired changes for the values of a & b in caller
Module Summary • Swapping the value of c & d instead of a & b • It is correct, but C++ has a better way out
Programming in Modern C++ Partha Pratim Das M07.11
Program 07.04: Swap in C & C++

Module M07
C Program: Call-by-value – wrong C++ Program: Call-by-reference – right

L
Partha Pratim
Das #include <stdio.h> #include <iostream>
using namespace std;

E
Objectives & void swap(int, int); // Call-by-value void swap(int&, int&); // Call-by-reference
Outlines
int main() { int a = 10, b = 15; int main() { int a = 10, b = 15;

T
Reference printf("a= %d & b= %d to swap\n",a,b); cout<<"a= "<<a<<" & b= "<<b<<"to swap"<<endl;
Pitfalls swap(a, b); swap(a, b); // Natural call
printf("a= %d & b= %d on swap\n",a,b); cout<<"a= "<<a<<" & b= "<<b<<"on swap"<<endl;

P
Call-by-Reference
Swap in C } }
Swap in C++ void swap(int c, int d) { int t ; void swap(int &x, int &y) { int t ;

N
const Reference t = c; c = d; d = t; t = x; x = y; y = t;
Parameter
} }
Return-by-
Reference
Pitfalls
• a= 10 & b= 15 to swap • a= 10 & b= 15 to swap
I/O Params of a • a= 10 & b= 15 on swap // No swap • a= 15 & b= 10 on swap // Correct swap
Function

Recommended
Mechanisms • Passing values of a=10 & b=15 • Passing values of a = 10 & b = 15
References vs. • In callee; c = 10 & d = 15 • In callee: x = 10 & y = 15
Pointers • Swapping the values of c & d • Swapping the values of x & y
Module Summary
• No change for the values of a & b in caller • Desired changes for the values of a & b in caller
• Here c & d do not share address with a & b • x & y having same address as a & b respectively
Programming in Modern C++ Partha Pratim Das M07.12
Program 07.05: Reference Parameter as const

Module M07
• A reference parameter may get changed in the called function
• Use const to stop reference parameter being changed

L
Partha Pratim
Das
const reference – bad const reference – good

E
Objectives &
Outlines #include <iostream> #include <iostream>

T
Reference using namespace std; using namespace std;
Pitfalls
int Ref_const(const int &x) { int Ref_const(const int &x) {

P
Call-by-Reference
Swap in C
++x; // Not allowed
Swap in C++
return (x); return (x + 1);

N
const Reference } }
Parameter int main() { int a = 10, b; int main() { int a = 10, b;
Return-by- b = Ref_const(a); b = Ref_const(a);
Reference cout << "a = " << a <<" and" cout << "a = " << a << " and"
Pitfalls << " b = " << b; << " b = " << b;
I/O Params of a } }
Function

Recommended
Mechanisms
• Error: Increment of read only Reference ’x’ a = 10 and b = 11
References vs.
Pointers
• Compilation Error: Value of x cannot be changed • No violation
Module Summary • Implies, a cannot be changed through x
Programming in Modern C++ Partha Pratim Das M07.13
Return-by-Reference

Module M07

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function Return-by-Reference
Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.14


Program 07.06: Return-by-Reference

Module M07 • A function can return a value by reference (Return-by-Reference)


• C uses Return-by-value

L
Partha Pratim
Das Return-by-value Return-by-reference

E
Objectives & #include <iostream> #include <iostream>
Outlines using namespace std; using namespace std;

T
Reference int Function_Return_By_Val(int &x) { int& Function_Return_By_Ref(int &x) {
Pitfalls cout << "x = " << x << " &x = " << &x << endl; cout << "x = " << x << " &x = " << &x << endl;
return (x); return (x);

P
Call-by-Reference
Swap in C
} }
Swap in C++
int main() { int a = 10; int main() { int a = 10;

N
const Reference cout << "a = " << a << " &a = " << &a << endl; cout << "a = " << a << " &a = " << &a << endl;
Parameter const int& b = // const needed. Why? const int& b = // const optional
Return-by- Function_Return_By_Val(a); Function_Return_By_Ref(a);
Reference cout << "b = " << b << " &b = " << &b << endl; cout << "b = " << b << " &b = " << &b << endl;
Pitfalls } }
I/O Params of a
Function
a = 10 &a = 00DCFD18 a = 10 &a = 00A7F8FC
Recommended
Mechanisms
x = 10 &x = 00DCFD18 x = 10 &x = 00A7F8FC
b = 10 &b = 00DCFD00 // Reference to temporary b = 10 &b = 00A7F8FC // Reference to a
References vs.
Pointers

Module Summary • Returned variable is temporary • Returned variable is an alias of a


• Has a different address • Has the same address
Programming in Modern C++ Partha Pratim Das M07.15
Program 07.07: Return-by-Reference can get tricky

Module M07 Return-by-reference Return-by-reference – Risky!

L
Partha Pratim #include <iostream> #include <iostream>
Das using namespace std; using namespace std;
int& Return_ref(int &x) { int& Return_ref(int &x) {

E
Objectives &
Outlines
int t = x;
t++;

T
Reference return (x); return (t);
Pitfalls
} }

P
Call-by-Reference int main() { int a = 10, b = Return_ref(a); int main() { int a = 10, b = Return_ref(a);
Swap in C cout << "a = " << a << " and b = " cout << "a = " << a << " and b = "
Swap in C++ << b << endl; << b << endl;

N
const Reference
Parameter
Return_ref(a) = 3; // Changes variable a Return_ref(a) = 3; // Changes local t
Return-by- cout << "a = " << a; cout << "a = " << a;
Reference
Pitfalls
} }
I/O Params of a
Function a = 10 and b = 10 a = 10 and b = 11
Recommended a=3 a = 10
Mechanisms

References vs.
Pointers • Note how a value is assigned to function call • We expect a to be 3, but it has not changed
Module Summary • This can change a local variable • It returns reference to local. This is risky

Programming in Modern C++ Partha Pratim Das M07.16


I/O Parameters of a Function

Module M07

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function I/O Parameters of a Function
Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.17


I/O of a Function

Module M07

• In C++ we can change values with a function as follows:

L
Partha Pratim
Das

E
Objectives &
Outlines
I/O of Function Purpose Mechanism
Value Parameter Input Call-by-value

T
Reference
Pitfalls Reference Parameter In-Out Call-by-reference

P
Call-by-Reference
Swap in C
const Reference Parameter Input Call-by-reference
Swap in C++ Return Value Output Return-by-value

N
const Reference
Parameter Return-by-reference
Return-by-
Reference
const Return-by-reference
Pitfalls

I/O Params of a
Function • In addition, we can use the Call-by-address (Call-by-value with pointer) and
Recommended Return-by-address (Return-by-value with pointer) as in C
Mechanisms

References vs. • But it is neither required nor advised


Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.18


Recommended Mechanisms

Module M07

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function Recommended Mechanisms
Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.19


Recommended Mechanisms

Module M07

• Call

L
Partha Pratim
Das
◦ Pass parameters of built-in types by value

E
Objectives &
Outlines . Recall: Array parameters are passed by reference in C and C++

T
Reference
Pitfalls
◦ Pass parameters of user-defined types by reference
. Make a reference parameter const if it is not used for output

P
Call-by-Reference
Swap in C
Swap in C++ • Return

N
const Reference
Parameter ◦ Return built-in types by value
Return-by-
Reference
◦ Return user-defined types by reference
Pitfalls
. Return value is not copied back
I/O Params of a
Function . May be faster than returning a value
Recommended . Beware: Calling function can change returned object
Mechanisms
. Never return a local variables by reference
References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.20


Difference between Reference and Pointer

Module M07

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Reference
Pitfalls

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function Difference between Reference and Pointer
Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.21


Difference between Reference and Pointer

Module M07
Pointers References

L
Partha Pratim
Das • Refers to an address (exposed) • Refers to an address (hidden)
• Pointers can point to NULL • References cannot be NULL

E
Objectives &
Outlines

T
Reference int *p = NULL; // p is not pointing int &j ; // wrong
Pitfalls
• Pointers can point to different variables at • For a reference, its referent is fixed

P
Call-by-Reference
Swap in C different times
Swap in C++
int a, b, *p; int a, c, &b = a; // Okay

N
const Reference
Parameter p = &a; // p points to a ...
Return-by-
Reference
... &b = c // Error
Pitfalls p = &b; // p points to b
I/O Params of a
Function • NULL checking is required • Does not require NULL checking
Recommended
Mechanisms
• Makes code faster
References vs.
• Allows users to operate on the address • Does not allow users to operate on the address
Pointers • diff pointers, increment, etc. • All operations are interpreted for the referent
Module Summary • Array of pointers can be defined • Array of references not allowed
Programming in Modern C++ Partha Pratim Das M07.22
Module Summary

Module M07

• Introduced reference in C++

L
Partha Pratim
Das
• Studied the difference between call-by-value and call-by-reference

E
Objectives &
Outlines • Studied the difference between return-by-value and return-by-reference

T
Reference
Pitfalls • Discussed the difference between References and Pointers

P
Call-by-Reference
Swap in C
Swap in C++

N
const Reference
Parameter

Return-by-
Reference
Pitfalls

I/O Params of a
Function

Recommended
Mechanisms

References vs.
Pointers

Module Summary

Programming in Modern C++ Partha Pratim Das M07.23

You might also like