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

module-7

Module 07 focuses on programming in C++, specifically on references and pointers. It covers concepts such as call-by-reference, swap functions in C and C++, and return-by-reference, along with examples and pitfalls. The module aims to help students understand the differences between references and pointers and their applications in function parameters.

Uploaded by

venkatratnam
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)
6 views

module-7

Module 07 focuses on programming in C++, specifically on references and pointers. It covers concepts such as call-by-reference, swap functions in C and C++, and return-by-reference, along with examples and pitfalls. The module aims to help students understand the differences between references and pointers and their applications in function parameters.

Uploaded by

venkatratnam
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/ 16

Module 07

Intructors: Abir
Das and
Sourangshu
Bhattacharya
Module 07: Programming in C++
Objectives & Reference & Pointer
Outlines

Reference
variable

Call-by-reference Intructors: Abir Das and Sourangshu Bhattacharya


Swap in C
Swap in C++
const Reference
Parameter Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Return-by-
reference
{abir, sourangshu}@cse.iitkgp.ac.in
I/O of a Function

References vs.
Pointers Slides taken from NPTEL course on Programming in Modern C++
Summary
by Prof. Partha Pratim Das

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 1


Module Objectives

Module 07

Intructors: Abir • Understand References in C++


Das and
Sourangshu
Bhattacharya
• Compare and contrast References and Pointers
Objectives &
Outlines

Reference
variable

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

Return-by-
reference

I/O of a Function

References vs.
Pointers

Summary

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 2


Module Outline

Module 07

Intructors: Abir • Reference variable or Alias


Das and
Sourangshu
Bhattacharya
◦ Basic Notion
◦ Call-by-reference in C++
Objectives &
Outlines • Example: Swapping two number in C
Reference
variable
◦ Using Call-by-value
Call-by-reference ◦ Using Call-by-address
Swap in C
Swap in C++ • Call-by-reference in C++ in contrast to Call-by-value in C
const Reference
Parameter • Use of const in Alias / Reference
Return-by-
reference • Return-by-reference in C++ in contrast to Return-by-value in C
I/O of a Function
• Differences between References and Pointers
References vs.
Pointers

Summary

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 3


Reference

Module 07

Intructors: Abir • A reference is an alias / synonym for an existing variable


Das and
Sourangshu
Bhattacharya
int i = 15; // i is a variable
int &j = i; // j is a reference to i
Objectives &
Outlines

Reference i ← variable
variable

Call-by-reference
15 ← memory content
Swap in C 200 ← address &i = &j
Swap in C++
const Reference
j ← alias or reference
Parameter

Return-by-
reference

I/O of a Function

References vs.
Pointers

Summary

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 4


Program 07.01: Behavior of Reference
#include <iostream>
Module 07
using namespace std;
Intructors: Abir
Das and int main() {
Sourangshu
Bhattacharya int a = 10, &b = a; // b is reference of a

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


Outlines cout << "a = " << a << ", b = " << b << ". " << "&a = " << &a << ", &b = " << &b << endl;
Reference
variable ++a; // Changing a appears as change in b
cout << "a = " << a << ", b = " << b << endl;
Call-by-reference
Swap in C
Swap in C++
++b; // Changing b also changes a
const Reference
cout << "a = " << a << ", b = " << b << endl;
Parameter }
Return-by-
reference
a = 10, b = 10. &a = 002BF944, &b = 002BF944
I/O of a Function
a = 11, b = 11
References vs. a = 12, b = 12
Pointers

Summary
• a and b have the same memory location and hence the same value
• Changing one changes the other and vice-versa

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 5


Pitfalls in Reference

Module 07 Wrong declaration Reason Correct declaration


Intructors: Abir
Das and int& i; no variable (address) to refer to – must be initialized int& i = j;
Sourangshu
Bhattacharya
int& j = 5; no address to refer to as 5 is a constant const int& j = 5;
int& i = j + k; only temporary address (result of j + k) to refer to const int& i = j + k;
Objectives &
Outlines
#include <iostream>
Reference
variable using namespace std;
Call-by-reference
Swap in C int main() {
Swap in C++ int i = 2;
const Reference
Parameter int& j = i;
Return-by-
const int& k = 5; // const tells compiler to allocate a memory with the value 5
reference const int& l = j + k; // Similarly for j + k = 7 for l to refer to
I/O of a Function

References vs. cout << i << ", " << &i << endl; // Prints: 2, 0x61fef8
Pointers cout << j << ", " << &j << endl; // Prints: 2, 0x61fef8
Summary cout << k << ", " << &k << endl; // Prints: 5, 0x61fefc
cout << l << ", " << &l << endl; // Prints: 7, 0x61ff00
}
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 6
C++ Program 07.02: Call-by-reference

Module 07 #include <iostream>


using namespace std;
Intructors: Abir
Das and
Sourangshu void Function_under_param_test( // Function prototype
Bhattacharya int&, // Reference parameter
int); // Value parameter
Objectives &
Outlines
int main() { int a = 20;
Reference cout << "a = " << a << ", &a = " << &a << endl << endl;
variable Function_under_param_test(a, a); // Function call
Call-by-reference }
Swap in C void Function_under_param_test(int &b, int c) { // Function definition
Swap in C++ cout << "b = " << b << ", &b = " << &b << endl << endl;
const Reference cout << "c = " << c << ", &c = " << &c << endl << endl;
Parameter
}
Return-by- ------- Output -------
reference
a = 20, &a = 0023FA30
I/O of a Function b = 20, &b = 0023FA30 // Address of b is same as a as b is a reference of a
References vs.
c = 20, &c = 0023F95C // Address different from a as c is a copy of a
Pointers • Param b is call-by-reference while param c is call-by-value
Summary • Actual param a and formal param b get the same value in called function
• Actual param a and formal param c get the same value in called function
• 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
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 7
C Program 07.03: Swap in C

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


Intructors: Abir
Das and
#include <stdio.h> #include <stdio.h>
Sourangshu
Bhattacharya void swap(int, int); // Call-by-value void swap(int *, int *); // Call-by-address
int main() { int a = 10, b = 15; int main() { int a=10, b=15;
Objectives & printf("a= %d & b= %d to swap\n", a, b); printf("a= %d & b= %d to swap\n", a, b);
Outlines
swap(a, b); swap(&a, &b); // Unnatural call
Reference printf("a= %d & b= %d on swap\n", a, b); printf("a= %d & b= %d on swap\n", a, b);
variable } }
Call-by-reference void swap(int c, int d) { int t; void swap(int *x, int *y) { int t;
Swap in C t = c; c = d; d = t; t = *x; *x = *y; *y = t;
Swap in C++ } }
const Reference
Parameter

Return-by- • a= 10 & b= 15 to swap • a= 10 & b= 15 to swap


reference
• a= 10 & b= 15 on swap // No swap • a= 15 & b= 10 on swap // Correct swap
I/O of a Function

References vs.
Pointers • Passing values of a=10 & b=15 • Passing Address of a & b
Summary
• In callee; c = 10 & d = 15 • In callee x = Addr(a) & y = Addr(b)
• 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
• Swapping the value of c & d instead of a & b • It is correct, but C++ has a better way out
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 8
Program 07.04: Swap in C & C++

Module 07
C Program: Call-by-value – wrong C++ Program: Call-by-reference – right
Intructors: Abir
Das and #include <stdio.h> #include <iostream>
Sourangshu using namespace std;
Bhattacharya
void swap(int, int); // Call-by-value void swap(int&, int&); // Call-by-reference
int main() { int a = 10, b = 15; int main() { int a = 10, b = 15;
Objectives &
Outlines printf("a= %d & b= %d to swap\n",a,b); cout<<"a= "<<a<<" & b= "<<b<<"to swap"<<endl;
swap(a, b); swap(a, b); // Natural call
Reference
variable
printf("a= %d & b= %d on swap\n",a,b); cout<<"a= "<<a<<" & b= "<<b<<"on swap"<<endl;
} }
Call-by-reference void swap(int c, int d) { int t ; void swap(int &x, int &y) { int t ;
Swap in C
t = c; c = d; d = t; t = x; x = y; y = t;
Swap in C++
} }
const Reference
Parameter

Return-by-
reference • a= 10 & b= 15 to swap • a= 10 & b= 15 to swap
• a= 10 & b= 15 on swap // No swap • a= 15 & b= 10 on swap // Correct swap
I/O of a Function

References vs.
Pointers
• Passing values of a=10 & b=15 • Passing values of a = 10 & b = 15
Summary • In callee; c = 10 & d = 15 • In callee: x = 10 & y = 15
• Swapping the values of c & d • Swapping the values of x & y
• 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
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 9
Program 07.05: Reference Parameter as const

Module 07
• A reference parameter may get changed in the called function
Intructors: Abir
Das and
• Use const to stop reference parameter being changed
Sourangshu const reference – bad const reference – good
Bhattacharya

#include <iostream> #include <iostream>


Objectives &
Outlines
using namespace std; using namespace std;

Reference int Ref_const(const int &x) { int Ref_const(const int &x) {


variable
++x; // Not allowed
Call-by-reference return (x); return (x + 1);
Swap in C } }
Swap in C++ int main() { int a = 10, b; int main() { int a = 10, b;
const Reference
Parameter
b = Ref_const(a); b = Ref_const(a);
cout << "a = " << a <<" and" cout << "a = " << a << " and"
Return-by-
reference
<< " b = " << b; << " b = " << b;
} }
I/O of a Function

References vs.
Pointers • Error: Increment of read only Reference ’x’ a = 10 and b = 11
Summary

• Compilation Error: Value of x cannot be changed • No violation


• Implies, a cannot be changed through x
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 10
Program 07.06: Return-by-reference

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


• C uses Return-by-value
Intructors: Abir
Das and Return-by-value Return-by-reference
Sourangshu
Bhattacharya
#include <iostream> #include <iostream>
using namespace std; using namespace std;
Objectives &
Outlines
int Function_Return_By_Val(int &x) { int& Function_Return_By_Ref(int &x) {
cout << "x = " << x << " &x = " << &x << endl; cout << "x = " << x << " &x = " << &x << endl;
Reference return (x); return (x);
variable
} }
Call-by-reference int main() { int a = 10; int main() { int a = 10;
Swap in C cout << "a = " << a << " &a = " << &a << endl; cout << "a = " << a << " &a = " << &a << endl;
Swap in C++
const int& b = // const needed. Why? const int& b = // const optional
const Reference
Parameter Function_Return_By_Val(a); Function_Return_By_Ref(a);
cout << "b = " << b << " &b = " << &b << endl; cout << "b = " << b << " &b = " << &b << endl;
Return-by-
reference
} }
I/O of a Function
a = 10 &a = 00DCFD18 a = 10 &a = 00A7F8FC
References vs.
Pointers x = 10 &x = 00DCFD18 x = 10 &x = 00A7F8FC
b = 10 &b = 00DCFD00 // Reference to temporary b = 10 &b = 00A7F8FC // Reference to a
Summary

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


• Has a different address • Has the same address
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 11
Program 07.07: Return-by-reference can get tricky

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

Intructors: Abir #include <iostream> #include <iostream>


Das and using namespace std; using namespace std;
Sourangshu
Bhattacharya int& Return_ref(int &x) { int& Return_ref(int &x) {
int t = x;
Objectives & t++;
Outlines return (x); return (t);
Reference
} }
variable int main() { int a = 10, b = Return_ref(a); int main() { int a = 10, b = Return_ref(a);
cout << "a = " << a << " and b = " cout << "a = " << a << " and b = "
Call-by-reference
Swap in C
<< b << endl; << b << endl;
Swap in C++
const Reference Return_ref(a) = 3; // Changes variable a Return_ref(a) = 3; // Changes local t
Parameter cout << "a = " << a; cout << "a = " << a;
Return-by- } }
reference

I/O of a Function
a = 10 and b = 10 a = 10 and b = 11
References vs. a=3 a = 10
Pointers

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

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 12


I/O of a Function

Module 07

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


Das and
Sourangshu
Bhattacharya
I/O of Function Purpose Mechanism
Objectives &
Outlines
Value Parameter Input Call-by-value
Reference
Reference Parameter In-Out Call-by-reference
variable
const Reference Parameter Input Call-by-reference
Call-by-reference
Swap in C
Return Value Output Return-by-value
Swap in C++ Return-by-reference
const Reference
Parameter const Return-by-reference
Return-by-
reference

I/O of a Function • In addition, we can use the Call-by-address (Call-by-value with pointer) and
References vs.
Pointers
Return-by-address (Return-by-value with pointer) as in C
Summary • But it is neither required nor advised

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 13


Recommended Mechanisms

Module 07

Intructors: Abir • Call


Das and
Sourangshu
Bhattacharya
◦ Pass parameters of built-in types by value
▷ Recall: Array parameters are passed by reference in C and C++
Objectives &
Outlines ◦ Pass parameters of user-defined types by reference
Reference
variable ▷ Make a reference parameter const if it is not used for output
Call-by-reference • Return
Swap in C
Swap in C++ ◦ Return built-in types by value
const Reference
Parameter ◦ Return user-defined types by reference
Return-by-
reference ▷ Return value is not copied back
I/O of a Function ▷ May be faster than returning a value
References vs. ▷ Beware: Calling function can change returned object
Pointers

Summary
▷ Never return a local variables by reference

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 14


Difference between Reference and Pointer

Module 07
Pointers References
Intructors: Abir
Das and • Refers to an address (exposed) • Refers to an address (hidden)
Sourangshu
Bhattacharya • Pointers can point to NULL • References cannot be NULL
Objectives &
Outlines int *p = NULL; // p is not pointing int &j ; // wrong
Reference • Pointers can point to different variables at • For a reference, its referent is fixed
variable
different times
Call-by-reference
Swap in C int a, b, *p; int a, c, &b = a; // Okay
Swap in C++
p = &a; // p points to a ...
const Reference
Parameter ... &b = c // Error
Return-by- p = &b; // p points to b
reference

I/O of a Function
• NULL checking is required • Does not require NULL checking
References vs.
Pointers • Makes code faster
Summary • Allows users to operate on the address • Does not allow users to operate on the address
• diff pointers, increment, etc. • All operations are interpreted for the referent
• Array of pointers can be defined • Array of references not allowed
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 15
Module Summary

Module 07

Intructors: Abir • Introduced reference in C++


Das and
Sourangshu
Bhattacharya
• Studied the difference between call-by-value and call-by-reference
Objectives &
• Studied the difference between return-by-value and return-by-reference
Outlines
• Discussed the difference between References and Pointers
Reference
variable

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

Return-by-
reference

I/O of a Function

References vs.
Pointers

Summary

CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 16

You might also like