You are on page 1of 26

Module 06

Partha Pratim
Das
Module 06: Programming in C++
Objectives &
Outline
Constants and Inline Functions
const-ness &
cv-qualifier
const-ness
Advantages Partha Pratim Das
Pointers
volatile

inline
Department of Computer Science and Engineering
functions Indian Institute of Technology, Kharagpur
Macros
inline ppd@cse.iitkgp.ernet.in
Summary

Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan

NPTEL MOOCs Programming in C++ Partha Pratim Das 1


Module Objectives

Module 06

Partha Pratim Understand const in C++ and contrast with Manifest


Das
Constants
Objectives &
Outline Understand inline in C++ and contrast with Macros
const-ness &
cv-qualifier
const-ness
Advantages
Pointers
volatile

inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 2


Module Outline

Module 06 const-ness and cv-qualifier


Partha Pratim Notion of const
Das Advantages of const
Objectives &
Natural Constants – π, e
Outline Program Constants – array size
const-ness & Prefer const to #define
cv-qualifier
const-ness
const and pointer
Advantages
Pointers
const-ness of pointer / pointee. How to decide?
volatile
Notion of volatile
inline
functions inline functions
Macros
inline Macros with params
Summary Advantages
Disadvantages
Notion of inline functions
Advantages

NPTEL MOOCs Programming in C++ Partha Pratim Das 3


Module 06: Lecture 08

Module 06 const-ness and cv-qualifier


Partha Pratim Notion of const
Das Advantages of const
Objectives &
Natural Constants – π, e
Outline Program Constants – array size
const-ness & Prefer const to #define
cv-qualifier
const-ness
const and pointer
Advantages
Pointers
const-ness of pointer / pointee. How to decide?
volatile

inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 4


Program 06.01: Manifest constants in C

Module 06 Manifest constants are defined by #define


Partha Pratim
Das
Manifest constants are replaced by CPP (C Pre-Processor)
Source Program Program after CPP
Objectives &
Outline #include <iostream> // Contents of <iostream> header replaced by CPP
#include <cmath> // Contents of <cmath> header replaced by CPP
const-ness & using namespace std; using namespace std;
cv-qualifier
const-ness #define TWO 2 // #define of TWO consumed by CPP
Advantages #define PI 4.0*atan(1.0) // #define of PI consumed by CPP
Pointers
volatile int main() { int main() {
int r = 10; int r = 10;
inline double peri = double peri =
functions TWO * PI * r; 2 * 4.0*atan(1.0) * r; // Replaced by CPP
Macros cout << "Perimeter = " cout << "Perimeter = "
inline << peri << endl; << peri << endl;
Summary return 0; return 0;
} }

Perimeter = 314.159 Perimeter = 314.159

• TWO is a manifest constant • CPP replaces the token TWO by 2


• PI is a manifest constant • CPP replaces the token PI by 4.0*atan(1.0)
• TWO & PI look like variables • Compiler sees them as constants

NPTEL MOOCs Programming in C++ Partha Pratim Das 5


Notion of const-ness

Module 06
The value of a const variable cannot be changed after definition
const int n = 10; // n is an int type variable with value 10
Partha Pratim // n is a constant
Das ...
n = 5; // Is a compilation error as n cannot be changed
Objectives & ...
Outline int m;
int *p = 0;
const-ness & p = &m; // Hold m by pointer p
cv-qualifier *p = 7; // Change m by p; m is now 7
const-ness ...
Advantages p = &n; // Is a compilation error as n may be changed by *p = 5;
Pointers
volatile
Naturally, a const variable must be initialized when defined
inline
functions const int n; // Is a compilation error as n must be initialized
Macros
inline
A variable of any data type can be declared as const
Summary
typedef struct _Complex {
double re;
double im;
} Complex;
const Complex c = {2.3, 7.5}; // c is a Complex type variable
// It is initialized with c.re = 2.3 and c.im = 7.5
// c is a constant
...
c.re = 3.5; // Is a compilation error as no part of c can be changed
NPTEL MOOCs Programming in C++ Partha Pratim Das 6
Program 06.02: Compare #define and const

Module 06 Using #define Using const

Partha Pratim #include <iostream> #include <iostream>


Das #include <cmath> #include <cmath>
using namespace std; using namespace std;
Objectives & #define TWO 2 const int TWO = 2;
Outline #define PI 4.0*atan(1.0) const double PI = 4.0*atan(1.0);
const-ness &
int main() { int main() {
cv-qualifier
int r = 10; int r = 10;
const-ness double peri = double peri =
Advantages
TWO * PI * r; TWO * PI * r; // No replacement by CPP
Pointers
cout << "Perimeter = " cout << "Perimeter = "
volatile
<< peri << endl; << peri << endl;
inline return 0; return 0;
functions } }
Macros
inline
Perimeter = 314.159 Perimeter = 314.159
Summary

• TWO is a manifest constant • TWO is a const variable initialized to 2


• PI is a manifest constant • PI is a const variable initialized to 4.0*atan(1.0)
• TWO & PI look like variables • TWO & PI are variables
• Types of TWO & PI may be indeterminate • Type of TWO is const int
• Type of PI is const double

NPTEL MOOCs Programming in C++ Partha Pratim Das 7


Advantages of const

Module 06
Natural Constants like π, e, Φ (Golden Ratio) etc. can be
compactly defined and used
Partha Pratim
Das const double pi = 4.0*atan(1.0); // pi = 3.14159
const double e = exp(1.0); // e = 2.71828
const double phi = (sqrt(5.0) + 1) / 2.0; // phi = 1.61803
Objectives &
Outline const int TRUE = 1; // Truth values
const int FALSE = 0;
const-ness &
cv-qualifier
const int null = 0; // null value
const-ness
Advantages
Pointers Note: NULL is a manifest constant in C/C++ set to 0.
volatile

inline Program Constants like number of elements, array size etc. can
functions be defined at one place (at times in a header) and used all over
Macros
inline the program
Summary const int nArraySize = 100;
const int nElements = 10;

int main() {
int A[nArraySize]; // Array size
for (int i = 0; i < nElements; ++i) // Number of elements
A[i] = i * i;

return 0;
}
NPTEL MOOCs Programming in C++ Partha Pratim Das 8
Advantages of const

Module 06 Prefer const over #define


Partha Pratim Using #define Using const
Das
Manifest Constant Constant Variable
Objectives &
Outline • Is not type safe • Has its type
• Replaced textually by CPP • Visible to the compiler
const-ness & • Cannot be watched in debugger • Can be watched in debugger
cv-qualifier • Evaluated as many times as replaced • Evaluated only on initialization
const-ness
Advantages
Pointers
volatile

inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 9


const and Pointers

Module 06 const-ness can be used with Pointers in one of the two ways:
Partha Pratim
Das
Pointer to Constant data where the pointee (pointed
data) cannot be changed
Objectives & Constant Pointer where the pointer (address) cannot be
Outline

const-ness &
changed
cv-qualifier
const-ness
Consider usual pointer-pointee computation (without const):
Advantages
Pointers int m = 4;
volatile int n = 5;
int * p = &n; // p points to n. *p is 5
inline ...
functions n = 6; // n and *p are 6 now
Macros *p = 7; // n and *p are 7 now. POINTEE changes
inline ...
Summary p = &m; // p points to m. *p is 4. POINTER changes
*p = 8; // m and *p are 8 now. n is 7. POINTEE changes

NPTEL MOOCs Programming in C++ Partha Pratim Das 10


const and Pointers: Pointer to Constant data

Module 06
Consider pointed data
Partha Pratim
Das int m = 4;
const int n = 5;
const int * p = &n;
Objectives & ...
Outline n = 6; // Error: n is constant and cannot be changed
*p = 7; // Error: p points to a constant data (n) that cannot be changed
const-ness & p = &m; // Okay
cv-qualifier *p = 8; // Okay
const-ness
Advantages
Pointers Interestingly,
volatile

inline int n = 5;
functions const int * p = &n;
...
Macros
n = 6; // Okay
inline
*p = 6; // Error: p points to a ’constant’ data (n) that cannot be changed
Summary
Finally,
const int n = 5;
int * p = &n; // Error: If this were allowed, we would be able to change constant n
...
n = 6; // Error: n is constant and cannot be changed
*p = 6; // Would have been okay, if declaration of p were valid

NPTEL MOOCs Programming in C++ Partha Pratim Das 11


const and Pointers: Constant Pointer

Module 06
Consider pointer
Partha Pratim int m = 4, n = 5;
Das int * const p = &n;
...
n = 6; // Okay
Objectives & *p = 7; // Okay. Both n and *p are 7 now
Outline ...
p = &m; // Error: p is a constant pointer and cannot be changed
const-ness &
cv-qualifier
const-ness By extension, both can be const
Advantages
Pointers const int m = 4;
volatile const int n = 5;
const int * const p = &n;
inline
...
functions
n = 6; // Error: n is constant and cannot be changed
Macros
*p = 7; // Error: p points to a ’constant’ data (n) that cannot be changed
inline
...
Summary p = &m; // Error: p is a constant pointer and cannot be changed

Finally, to decide on const-ness, draw a mental line through *


int n = 5;
int * p = &n; // non-const-Pointer to non-const-Pointee
const int * p = &n; // non-const-Pointer to const-Pointee
int * const p = &n; // const-Pointer to non-const-Pointee
const int * const p = &n; // const-Pointer to const-Pointee
NPTEL MOOCs Programming in C++ Partha Pratim Das 12
const and Pointers: The case of C-string

Module 06
Consider the example:
char * str = strdup("IIT, Kharagpur");
Partha Pratim str[0] = ’N’; // Edit the name
Das cout << str << endl;
str = strdup("JIT, Kharagpur"); // Change the name
Objectives & cout << str << endl;
Outline
Output is:
const-ness &
cv-qualifier NIT, Kharagpur
const-ness JIT, Kharagpur
Advantages
Pointers To stop editing the name:
volatile
const char * str = strdup("IIT, Kharagpur");
inline
str[0] = ’N’; // Error: Cannot Edit the name
functions
str = strdup("JIT, Kharagpur"); // Change the name
Macros
inline
To stop changing the name:
Summary
char * const str = strdup("IIT, Kharagpur");
str[0] = ’N’; // Edit the name
str = strdup("JIT, Kharagpur"); // Error: Cannot Change the name

To stop both:
const char * const str = strdup("IIT, Kharagpur");
str[0] = ’N’; // Error: Cannot Edit the name
str = strdup("JIT, Kharagpur"); // Error: Cannot Change the name
NPTEL MOOCs Programming in C++ Partha Pratim Das 13
Module 06: End of Lecture 08

Module 06 const-ness and cv-qualifier


Partha Pratim Notion of const
Das Advantages of const
Objectives &
Natural Constants – π, e
Outline Program Constants – array size
const-ness & Prefer const to #define
cv-qualifier
const-ness
const and pointer
Advantages
Pointers
const-ness of pointer / pointee. How to decide?
volatile

inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 14


Module 06: Lecture 09

Module 06

Partha Pratim Notion of Volatile


Das
Inline Function
Objectives &
Outline
Macros with parameters
const-ness &
Advantage
cv-qualifier Disadvantage
const-ness
Advantages Notion of inline
Pointers
volatile Advantage
inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 15


Notion of volatile

Module 06 Variable Read-Write


Partha Pratim The value of a variable can be read and / or assigned at
Das any point of time
Objectives &
The value assigned to a variable does not change till a
Outline next assignment is made (value is persistent)
const-ness &
cv-qualifier const
const-ness
Advantages
The value of a const variable can be set only at
Pointers
volatile
initialization – cannot be changed afterwards
inline volatile
functions
Macros In contrast, the value of a volatile variable may be
inline
different every time it is read – even if no assignment has
Summary
been made to it
A variable is taken as volatile if it can be changed by
hardware, the kernel, another thread etc.
cv-qualifier: A declaration may be prefixed with a
qualifier – const or volatile
NPTEL MOOCs Programming in C++ Partha Pratim Das 16
Using volatile

Module 06
Consider:
Partha Pratim static int i;
Das void fun(void) {
i = 0;
while (i != 100);
Objectives & }
Outline

const-ness & This is an infinite loop! Hence the compiler should optimize as:
cv-qualifier
const-ness static int i;
Advantages void fun(void) {
Pointers i = 0;
volatile while (1); // Compiler optimizes
}
inline
functions
Macros Now qualify i as volatile:
inline
static volatile int i;
Summary
void fun(void) {
i = 0;
while (i != 100); // Compiler does not optimize
}

Being volatile, i can be changed by hardware anytime. It waits till


the value becomes 100 (possibly some hardware writes to a port).
NPTEL MOOCs Programming in C++ Partha Pratim Das 17
Program 06.03: Macros with Parameters

Module 06 Macros with Parameters are defined by #define


Partha Pratim
Das
Macros with Parameters are replaced by CPP
Source Program Program after CPP
Objectives &
Outline #include <iostream> // Contents of <iostream> header replaced by CPP
using namespace std; using namespace std;
const-ness &
cv-qualifier #define SQUARE(x) x * x // #define of SQUARE(x) consumed by CPP
const-ness
Advantages int main() { int main() {
Pointers int a = 3, b; int a = 3, b;
volatile
b = SQUARE(a); b = a * a; // Replaced by CPP
inline
functions cout << "Square = " cout << "Square = "
Macros << b << endl; << b << endl;
inline return 0; return 0;
} }
Summary

Square = 9 Square = 9

• SQUARE(x) is a macro with one param • CPP replaces the SQUARE(x) substituting x with a
• SQUARE(x) looks like a function • Compiler does not see it as function

NPTEL MOOCs Programming in C++ Partha Pratim Das 18


Pitfalls of macros

Module 06
Consider the example:
#include <iostream>
Partha Pratim
using namespace std;
Das
#define SQUARE(x) x * x
Objectives &
Outline int main() {
int a = 3, b;
const-ness &
cv-qualifier b = SQUARE(a + 1); // Wrong macro expansion
const-ness
Advantages cout << "Square = " << b << endl;
Pointers
volatile return 0;
}
inline
functions
Macros Output is 7 in stead of 16 as expected. On the expansion line it gets:
inline
b = a + 1 * a + 1;
Summary

To fix:
#define SQUARE(x) (x) * (x)

Now:
b = (a + 1) * (a + 1);
NPTEL MOOCs Programming in C++ Partha Pratim Das 19
Pitfalls of macros

Module 06
Continuing ...
Partha Pratim #include <iostream>
Das using namespace std;

#define SQUARE(x) (x) * (x)


Objectives &
Outline int main() {
int a = 3, b;
const-ness &
cv-qualifier b = SQUARE(++a);
const-ness
Advantages cout << "Square = " << b << endl;
Pointers
volatile return 0;
inline }
functions
Macros Output is 25 in stead of 16 as expected. On the expansion line it gets:
inline

Summary b = (++a) * (++a);

and a is incremented twice before being used! There is no easy fix.

NPTEL MOOCs Programming in C++ Partha Pratim Das 20


inline Function

Module 06

Partha Pratim An inline function is just another functions


Das
The function prototype is preceded by the keyword inline
Objectives &
Outline
An inline function is expanded (inlined) at the site of its call
const-ness &
cv-qualifier and the overhead of passing parameters between caller and
const-ness callee (or called) functions is avoided
Advantages
Pointers
volatile

inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 21


Program 06.04: Macros as inline Functions

Module 06 Define the function


Partha Pratim
Das
Prefix function header with inline
Compile function body and function call together
Objectives &
Outline Using macro Using inline
const-ness & #include <iostream> #include <iostream>
cv-qualifier using namespace std; using namespace std;
const-ness #define SQUARE(x) x * x inline int SQUARE(int x) { return x * x; }
Advantages int main() { int main() {
Pointers int a = 3, b; int a = 3, b;
volatile b = SQUARE(a); b = SQUARE(a);
cout << "Square = " cout << "Square = "
inline
<< b << endl; << b << endl;
functions
return 0; return 0;
Macros
} }
inline

Summary
Square = 9 Square = 9

• SQUARE(x) is a macro with one param • SQUARE(x) is a function with one param
• Macro SQUARE(x) is efficient • inline SQUARE(x) is equally efficient
• SQUARE(a + 1) fails • SQUARE(a + 1) works
• SQUARE(++a) fails • SQUARE(++a) works
• SQUARE(++a) does not check type • SQUARE(++a) checks type

NPTEL MOOCs Programming in C++ Partha Pratim Das 22


Macros & inline Functions:
Compare and Contrast
Module 06 Macros inline Functions

Partha Pratim • Expanded at the place of calls • Expanded at the place of calls
Das • Efficient in execution • Efficient in execution
• Code bloats • Code bloats
• Has syntactic and semantic pitfalls • No pitfall
Objectives &
• Type checking for parameters is not done • Type checking for parameters is robust
Outline
• Helps to write max / swap for all types • Needs template for the same purpose
const-ness & • Errors are not checked during compilation • Errors are checked during compilation
cv-qualifier • Not available to debugger • Available to debugger in DEBUG build
const-ness
Advantages
Pointers
volatile

inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 23


Limitations of Function inlineing

Module 06

Partha Pratim inlineing is a directive – compiler may not inline


Das
functions with large body
Objectives &
Outline inline functions may not be recursive
const-ness &
cv-qualifier
Function body is needed for inlineing at the time of
const-ness function call. Hence, implementation hiding is not
Advantages
Pointers possible. Implement inline functions in header files
volatile

inline inline functions must not have two different definitions


functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 24


Module Summary

Module 06

Partha Pratim Revisit manifest constants from C


Das
Understand const-ness, its use and advantages over
Objectives &
Outline manifest constants
const-ness &
cv-qualifier
Understand the interplay of const and pointer
const-ness
Advantages
Understand the notion and use of volatile data
Pointers
volatile Revisit macros with parameters from C
inline
functions Understand inline functions and their advantages over
Macros
inline
macros
Summary Limitations of inlineing

NPTEL MOOCs Programming in C++ Partha Pratim Das 25


Instructor and TAs

Module 06

Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor ppd@cse.iitkgp.ernet.in 9830030880
Outline Tanwi Mallick, TA tanwimallick@gmail.com 9674277774
const-ness &
Srijoni Majumdar, TA majumdarsrijoni@gmail.com 9674474267
cv-qualifier Himadri B G S Bhuyan, TA himadribhuyan@gmail.com 9438911655
const-ness
Advantages
Pointers
volatile

inline
functions
Macros
inline

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 26

You might also like