Module 6
Module 6
Intructors:
Abir Das and
Sourangshu Module 06: Programming in C++
Bhattacharya
Constants and Inline Functions
Objectives &
Outline
const-ness &
cv-qualifier
const-ness
Intructors: Abir Das and Sourangshu Bhattacharya
Advantages
Pointers
volatile Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
inline
functions
{abir, sourangshu}@cse.iitkgp.ac.in
Macros
inline
Summary
Slides taken from NPTEL course on Programming in C++
by Prof. Partha Pratim Das
Module 06
Intructors:
Understand const in C++ and contrast with Manifest
Abir Das and
Sourangshu
Constants
Bhattacharya
Understand inline in C++ and contrast with Macros
Objectives &
Outline
const-ness &
cv-qualifier
const-ness
Advantages
Pointers
volatile
inline
functions
Macros
inline
Summary
Module 06
const-ness and cv-qualifier
Notion of const
Intructors:
Abir Das and Advantages of const
Sourangshu
Bhattacharya Natural Constants – π, e
Program Constants – array size
Objectives & Prefer const to #define
Outline
const and pointer
const-ness &
cv-qualifier const-ness of pointer / pointee. How to decide?
const-ness
Advantages Notion of volatile
Pointers
volatile inline functions
inline Macros with params
functions
Macros Advantages
inline Disadvantages
Summary Notion of inline functions
Advantages
return 0;
}
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 7
Advantages of const
inline
functions
Macros
inline
Summary
Module 06 const-ness can be used with Pointers in one of the two ways:
Intructors: Pointer to Constant data where the pointee (pointed
Abir Das and
Sourangshu data) cannot be changed
Bhattacharya
Constant Pointer where the pointer (address) cannot be
Objectives &
changed
Outline
Consider usual pointer-pointee computation (without const):
const-ness &
cv-qualifier int m = 4;
const-ness int n = 5;
Advantages int * p = &n; // p points to n. *p is 5
Pointers ...
volatile n = 6; // n and *p are 6 now
*p = 7; // n and *p are 7 now. POINTEE changes
inline ...
functions p = &m; // p points to m. *p is 4. POINTER changes
Macros *p = 8; // m and *p are 8 now. n is 7. POINTEE changes
inline
Summary
Module 06
Consider pointed data
Intructors:
Abir Das and int m = 4;
Sourangshu const int n = 5;
Bhattacharya const int * p = &n;
...
n = 6; // Error: n is constant and cannot be changed
Objectives & *p = 7; // Error: p points to a constant data (n) that cannot be changed
Outline p = &m; // Okay
*p = 8; // Error: even though m is not constant, it cannot be changed through p
const-ness &
cv-qualifier
const-ness Interestingly,
Advantages
Pointers int n = 5;
volatile const int * p = &n;
...
inline n = 6; // Okay
functions *p = 6; // Error: p points to a ’constant’ data (n) that cannot be changed
Macros
inline
Finally,
Summary
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
inline
functions
Macros
inline
Summary
inline
functions Standard g++ compiler prints: 5 0x16b58f4ec 0x16b58f4ec 10
Macros b actually points to a
inline
But when accessed through a the compiler substitutes the constant expression
Summary Technically the behavior is undefined
Module 06
Consider pointer
Intructors: int m = 4, n = 5;
Abir Das and int * const p = &n;
Sourangshu ...
Bhattacharya n = 6; // Okay
*p = 7; // Okay. Both n and *p are 7 now
...
Objectives & p = &m; // Error: p is a constant pointer and cannot be changed
Outline
Module 06
Consider the example:
char * str = strdup("IIT, Kharagpur");
Intructors: str[0] = ’N’; // Edit the name
Abir Das and cout << str << endl;
Sourangshu str = strdup("JIT, Kharagpur"); // Change the name
Bhattacharya cout << str << endl;
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
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 14
Notion of volatile
Module 06
Variable Read-Write
The value of a variable can be read and / or assigned at
Intructors:
Abir Das and any point of time
Sourangshu
Bhattacharya The value assigned to a variable does not change till a
next assignment is made (value is persistent)
Objectives &
Outline const
const-ness & The value of a const variable can be set only at
cv-qualifier
const-ness
initialization – cannot be changed afterwards
Advantages
Pointers
volatile
volatile
In contrast, the value of a volatile variable may be
inline
functions different every time it is read – even if no assignment has
Macros been made to it
inline
A variable is taken as volatile if it can be changed by
Summary
hardware, the kernel, another thread etc.
cv-qualifier: A declaration may be prefixed with a
qualifier – const or volatile
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 15
Using volatile
Module 06
Consider:
Intructors: static int i;
Abir Das and void fun(void) {
Sourangshu i = 0;
Bhattacharya while (i != 100);
}
Objectives &
Outline
This is an infinite loop! Hence the compiler should optimize as:
const-ness & static int i;
cv-qualifier void fun(void) {
const-ness i = 0;
Advantages
while (1); // Compiler optimizes
Pointers
}
volatile
inline
Now qualify i as volatile:
functions
static volatile int i;
Macros
void fun(void) {
inline
i = 0;
Summary while (i != 100); // Compiler does not optimize
}
• 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
Module 06
Consider the example:
#include <iostream>
Intructors:
using namespace std;
Abir Das and
Sourangshu
#define SQUARE(x) x * x
Bhattacharya
int main() {
Objectives & int a = 3, b;
Outline
b = SQUARE(a + 1); // Wrong macro expansion
const-ness &
cv-qualifier cout << "Square = " << b << endl;
const-ness
Advantages return 0;
Pointers }
volatile
inline
Output is 7 in stead of 16 as expected. On the expansion line it gets:
functions
b = a + 1 * a + 1;
Macros
inline
To fix:
Summary
#define SQUARE(x) (x) * (x)
Now:
b = (a + 1) * (a + 1);
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 18
Pitfalls of macros
Module 06
Continuing ...
Intructors: #include <iostream>
Abir Das and using namespace std;
Sourangshu
Bhattacharya #define SQUARE(x) (x) * (x)
int main() {
Objectives & int a = 3, b;
Outline
b = SQUARE(++a);
const-ness &
cv-qualifier cout << "Square = " << b << endl;
const-ness
Advantages return 0;
Pointers }
volatile
Summary and a is incremented twice before being used! There is no easy fix.
Module 06
inline
functions
Macros
inline
Summary
Summary • 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
inline
functions
Macros
inline
Summary
Module 06
Intructors:
inlineing is a directive – compiler may not inline
Abir Das and
Sourangshu
functions with large body
Bhattacharya
inline functions may not be recursive
Objectives &
Outline
Function body is needed for inlineing at the time of
const-ness & function call. Hence, implementation hiding is not
cv-qualifier
const-ness
possible. Implement inline functions in header files
Advantages
Pointers
inline functions must not have two different definitions
volatile
inline
functions
Macros
inline
Summary
Module 06
Intructors:
Revisit manifest constants from C
Abir Das and
Sourangshu Understand const-ness, its use and advantages over
Bhattacharya
manifest constants
Objectives &
Outline
Understand the interplay of const and pointer
const-ness & Understand the notion and use of volatile data
cv-qualifier
const-ness
Advantages
Revisit macros with parameters from C
Pointers
volatile
Understand inline functions and their advantages over
inline macros
functions
Macros Limitations of inlineing
inline
Summary