0% found this document useful (0 votes)
12 views25 pages

Module 06 CV - Inline

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)
12 views25 pages

Module 06 CV - Inline

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/ 25

Module M06

L
Partha Pratim
Das
Programming in Modern C++

E
Weekly Recap

Objectives & Module M06: Constants and Inline Functions

T
Outline

cv-qualifier:

P
const &
volatile
const
Partha Pratim Das

N
Advantages
Pointers
C-String
Department of Computer Science and Engineering
volatile
Indian Institute of Technology, Kharagpur
inline functions
Macros [email protected]
Pitfalls
inline
Comparison
All url’s in this module have been accessed in September, 2021 and found to be functional
Limitations

Module Summary

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


Weekly Recap

Module M06

• Understood the importance and ease of C++ in programming

L
Partha Pratim
Das
• KYC - Pre-requisites, Outline, Evaluation and Textbooks and References

E
Weekly Recap

Objectives &
• Understood some fundamental differences between C & C++:

T
Outline
◦ IO, Variable declaration, and Loops
cv-qualifier:
◦ Arrays and Strings

P
const &
volatile
const ◦ Sorting and Searching

N
Advantages
Pointers
◦ Stack and Common Containers in C++
C-String ◦ Various Standard Library in C and in C++
volatile

inline functions
Macros
Pitfalls
inline
Comparison
Limitations

Module Summary

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


Module Objectives

Module M06

• Understand const in C++ and contrast with Manifest Constants

L
Partha Pratim
Das
• Understand inline in C++ and contrast with Macros

E
Weekly Recap

Objectives &

T
Outline

cv-qualifier:

P
const &
volatile
const

N
Advantages
Pointers
C-String
volatile

inline functions
Macros
Pitfalls
inline
Comparison
Limitations

Module Summary

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


Module Outline

Module M06

1 Weekly Recap

L
Partha Pratim
Das

2 cv-qualifier: const & volatile

E
Weekly Recap

Objectives & Notion of const

T
Outline
Advantages of const
cv-qualifier:
const and pointer

P
const &
volatile
C-String
const
Notion of volatile

N
Advantages
Pointers
C-String
volatile
3 inline functions
inline functions Macros with Params in C
Macros Pitfalls of Macros
Pitfalls
inline Notion of inline
Comparison Comparison of Macros and inline Functions
Limitations Limitations of inline Functions
Module Summary
4 Module Summary
Programming in Modern C++ Partha Pratim Das M06.4
const-ness and cv-qualifier

Module M06

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

cv-qualifier:

P
const &
volatile
const

N
Advantages
Pointers
C-String
volatile

inline functions
Macros
Pitfalls
inline
const-ness and cv-qualifier
Comparison
Limitations

Module Summary

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


Program 06.01: Manifest constants in C / C++

Module M06 • Manifest constants are defined by #define


• Manifest constants are replaced by CPP (C Pre-Processor). Check Tutorial on C Preprocessor (CPP)

L
Partha Pratim
Das Source Program Program after CPP

E
Weekly Recap #include <iostream> // Contents of <iostream> header replaced by CPP
Objectives & #include <cmath> // Contents of <cmath> header replaced by CPP

T
Outline using namespace std; using namespace std;
cv-qualifier:
#define TWO 2 // Manifest const // #define of TWO consumed by CPP

P
const &
volatile #define PI 4.0*atan(1.0) // Const expr. // #define of PI consumed by CPP
const

N
Advantages
int main() { int r = 10; int main() { int r = 10;
Pointers
double peri = TWO * PI * r; double peri = 2 * 4.0*atan(1.0) * r; // By CPP
C-String
cout << "Perimeter = " << peri << endl; cout << "Perimeter = " << peri << endl;
volatile
} }
inline functions
Macros
Pitfalls
Perimeter = 62.8319 Perimeter = 62.8319
inline
Comparison
Limitations
• TWO is a manifest constant • CPP replaces the token TWO by 2
Module Summary • PI is a manifest constant as macro • CPP replaces the token PI by 4.0*atan(1.0) and evaluates
• TWO & PI look like variables • Compiler sees them as constants
• TWO * PI = 6.28319 by constant folding of compiler
Programming in Modern C++ Partha Pratim Das M06.6
Notion of const-ness

Module M06
• The value of a const variable cannot be changed after definition
const int n = 10; // n is an int type variable with value 10. n is a constant

L
Partha Pratim
Das ...
n = 5; // Is a compilation error as n cannot be changed

E
Weekly Recap ...
Objectives &
int m;

T
Outline int *p = 0;
p = &m; // Hold m by pointer p
cv-qualifier:
*p = 7; // Change m by p; m is now 7

P
const &
volatile ...
const p = &n; // Is a compilation error as n may be changed by *p = 5;

N
Advantages
Pointers
• Naturally, a const variable must be initialized when defined
C-String
const int n; // Is a compilation error as n must be initialized
volatile

inline functions
• A variable of any data type can be declared as const
Macros typedef struct _Complex {
Pitfalls double re;
inline double im;
Comparison
} Complex;
Limitations
const Complex c = {2.3, 7.5}; // c is a Complex type variable
Module Summary // 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
Programming in Modern C++ Partha Pratim Das M06.7
Program 06.02: Compare #define and const
Using #define Using const
Module M06
#include <iostream> #include <iostream>

L
Partha Pratim
Das #include <cmath> #include <cmath>
using namespace std; using namespace std;

E
Weekly Recap

Objectives &
#define TWO 2 const int TWO = 2;

T
Outline #define PI 4.0*atan(1.0) const double PI = 4.0*atan(1.0);
cv-qualifier:
int main() { int r = 10; int main() { int r = 10;

P
const &
volatile // Replace by CPP // No replacement by CPP
const double peri = 2 * 4.0*atan(1.0) * r; double peri = TWO * PI * r;

N
Advantages cout << "Perimeter = " << peri << endl; cout << "Perimeter = " << peri << endl;
Pointers
} }
C-String
volatile

inline functions Perimeter = 62.8319 Perimeter = 62.8319


Macros
Pitfalls
inline
• TWO is a manifest constant • TWO is a const variable initialized to 2
Comparison
• PI is a manifest constant • PI is a const variable initialized to 4.0*atan(1.0)
Limitations
• TWO & PI look like variables • TWO & PI are variables
Module Summary • Types of TWO & PI may be indeterminate • Type of TWO is const int
• TWO * PI = 6.28319 by constant folding of compiler • Type of PI is const double

Programming in Modern C++ Partha Pratim Das M06.8


Advantages of const

Module M06
• Natural Constants like π, e, Φ (Golden Ratio) etc. can be compactly defined and used

L
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

E
Weekly Recap

Objectives &
const int TRUE = 1; // Truth values

T
Outline
const int FALSE = 0;
cv-qualifier:

P
const &
volatile const int null = 0; // null value
const
Note: NULL is a manifest constant in C/C++ set to 0

N
Advantages
Pointers
C-String • Program Constants like number of elements, array size etc. can be defined at one place (at
volatile times in a header) and used all over the program
inline functions
const int nArraySize = 100;
Macros
const int nElements = 10;
Pitfalls
inline
Comparison
int main() {
Limitations int A[nArraySize]; // Array size
for (int i = 0; i < nElements; ++i) // Number of elements
Module Summary
A[i] = i * i;
}

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


Advantages of const

Module M06
• Prefer const over #define

L
Partha Pratim
Das
Using #define Using const

E
Weekly Recap

Objectives &
Manifest Constant Constant Variable

T
Outline

cv-qualifier:
• •

P
const &
volatile
Is not type safe Has its type
const • Replaced textually by CPP • Visible to the compiler

N
Advantages
Pointers
• Cannot be watched in debugger • Can be watched in debugger
C-String • Evaluated as many times as replaced • Evaluated only on initialization
volatile

inline functions
Macros
Pitfalls
inline
Comparison
Limitations

Module Summary

Programming in Modern C++ Partha Pratim Das M06.10


const and Pointers

Module M06

• const-ness can be used with Pointers in one of the two ways:

L
Partha Pratim
Das
◦ Pointer to Constant data where the pointee (pointed data) cannot be changed

E
Weekly Recap
◦ Constant Pointer where the pointer (address) cannot be changed
Objectives &

T
Outline • Consider usual pointer-pointee computation (without const):
cv-qualifier: int m = 4;

P
const &
volatile int n = 5;
const int * p = &n; // p points to n. *p is 5

N
Advantages ...
Pointers n = 6; // n and *p are 6 now
C-String *p = 7; // n and *p are 7 now. POINTEE changes
volatile ...
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
Pitfalls
inline
Comparison
Limitations

Module Summary

Programming in Modern C++ Partha Pratim Das M06.11


const and Pointers: Pointer to Constant data

Module M06
Consider pointed data

L
Partha Pratim
Das
int m = 4;
const int n = 5;

E
Weekly Recap const int * p = &n;
...
Objectives &
n = 6; // Error: n is constant and cannot be changed

T
Outline
*p = 7; // Error: p points to a constant data (n) that cannot be changed
cv-qualifier: p = &m; // Okay

P
const &
volatile *p = 8; // Error: p points to a constant data. Its pointee cannot be changed
const Interestingly,

N
Advantages
Pointers int n = 5;
C-String const int * p = &n;
volatile ...
inline functions n = 6; // Okay
Macros *p = 6; // Error: p points to a constant data (n) that cannot be changed
Pitfalls
inline
Finally,
Comparison const int n = 5;
Limitations int *p = &n; // Error: If this were allowed, we would be able to change constant n
Module Summary ...
n = 6; // Error: n is constant and cannot be changed
*p = 6; // Would have been okay, if declaration of p were valid
Programming in Modern C++ Partha Pratim Das M06.12
const and Pointers: Constant Pointer

Module M06
Consider pointer
int m = 4, n = 5;

L
Partha Pratim
Das
int * const p = &n;
...

E
Weekly Recap n = 6; // Okay
*p = 7; // Okay. Both n and *p are 7 now
Objectives &
...

T
Outline
p = &m; // Error: p is a constant pointer and cannot be changed
cv-qualifier:
By extension, both can be const

P
const &
volatile
const const int m = 4;

N
Advantages const int n = 5;
Pointers const int * const p = &n;
C-String ...
volatile n = 6; // Error: n is constant and cannot be changed
inline functions *p = 7; // Error: p points to a constant data (n) that cannot be changed
Macros ...
Pitfalls p = &m; // Error: p is a constant pointer and cannot be changed
inline
Comparison
Finally, to decide on const-ness, draw a mental line through *
Limitations int n = 5;
Module Summary 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
Programming in Modern C++ Partha Pratim Das M06.13
const and Pointers: The case of C-string

Module M06 Consider the example:


char * str = strdup("IIT, Kharagpur");

L
Partha Pratim
Das str[0] = ’N’; // Edit the name
cout << str << endl;

E
Weekly Recap str = strdup("JIT, Kharagpur"); // Change the name
Objectives & cout << str << endl;

T
Outline
Output is:
cv-qualifier:
NIT, Kharagpur

P
const &
volatile JIT, Kharagpur
const
To stop editing the name:

N
Advantages
Pointers const char * str = strdup("IIT, Kharagpur");
C-String str[0] = ’N’; // Error: Cannot Edit the name
volatile str = strdup("JIT, Kharagpur"); // Change the name
inline functions
Macros
To stop changing the name:
Pitfalls char * const str = strdup("IIT, Kharagpur");
inline str[0] = ’N’; // Edit the name
Comparison str = strdup("JIT, Kharagpur"); // Error: Cannot Change the name
Limitations
To stop both:
Module Summary
const char * const str = strdup("IIT, Kharagpur");
str[0] = ’N’; // Error: Cannot Edit the name
str = strdup("JIT, Kharagpur"); // Error: Cannot Change the name
Programming in Modern C++ Partha Pratim Das M06.14
Notion of volatile

Module M06

• Variable Read-Write

L
Partha Pratim
Das
◦ The value of a variable can be read and / or assigned at any point of time

E
Weekly Recap
◦ The value assigned to a variable does not change till a next assignment is made
Objectives &
(value is persistent)

T
Outline

cv-qualifier:
• const

P
const &
volatile
const ◦ The value of a const variable can be set only at initialization – cannot be changed

N
Advantages
Pointers
afterwards
C-String
volatile
• volatile
inline functions ◦ In contrast, the value of a volatile variable may be different every time it is read
Macros
Pitfalls
– even if no assignment has been made to it
inline ◦ A variable is taken as volatile if it can be changed by hardware, the kernel,
Comparison
Limitations
another thread etc.
Module Summary • cv-qualifier: A declaration may be prefixed with a qualifier – const or volatile

Programming in Modern C++ Partha Pratim Das M06.15


Using volatile

Module M06 Consider:


static int i;

L
Partha Pratim
Das void fun(void) {
i = 0;

E
Weekly Recap while (i != 100);
Objectives & }

T
Outline
This is an infinite loop! Hence the compiler should optimize as:
cv-qualifier:

P
const & static int i;
volatile void fun(void) {
const i = 0;

N
Advantages
while (1); // Compiler optimizes
Pointers
}
C-String
volatile Now qualify i as volatile:
inline functions static volatile int i;
Macros
void fun(void) {
Pitfalls
i = 0;
inline
Comparison
while (i != 100); // Compiler does not optimize
Limitations
}
Module Summary
Being volatile, i can be changed by hardware anytime. It waits till the value becomes 100
(possibly some hardware writes to a port).
Programming in Modern C++ Partha Pratim Das M06.16
inline functions

Module M06

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

cv-qualifier:

P
const &
volatile
const

N
Advantages
Pointers
C-String
volatile

inline functions
Macros
Pitfalls
inline
inline functions
Comparison
Limitations

Module Summary

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


Program 06.03: Macros with Parameters

Module M06
• Macros with Parameters are defined by #define
• Macros with Parameters are replaced by CPP

L
Partha Pratim
Das
Source Program Program after CPP

E
Weekly Recap

Objectives & #include <iostream> #include <iostream> // Header replaced by CPP

T
Outline using namespace std; using namespace std;
cv-qualifier:
#define SQUARE(x) x * x // #define of SQUARE(x) consumed by CPP

P
const &
volatile
const int main() { int main() {

N
Advantages int a = 3, b; int a = 3, b;
Pointers
C-String
b = SQUARE(a); b = a * a; // Replaced by CPP
volatile

inline functions cout << "Square = " << b << endl; cout << "Square = " << b << endl;
Macros } }
Pitfalls
inline
Comparison
Square = 9 Square = 9
Limitations

Module Summary
• 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
Programming in Modern C++ Partha Pratim Das M06.18
Pitfalls of macros

Module M06 Consider the example:


#include <iostream>

L
Partha Pratim
Das using namespace std;

E
Weekly Recap #define SQUARE(x) x * x
Objectives &

T
Outline int main() {
cv-qualifier: int a = 3, b;

P
const &
volatile b = SQUARE(a + 1); // Error: Wrong macro expansion
const

N
Advantages
cout << "Square = " << b << endl;
Pointers
}
C-String
volatile Output is 7 in stead of 16 as expected. On the expansion line it gets:
inline functions b = a + 1 * a + 1;
Macros
Pitfalls To fix:
inline
Comparison
#define SQUARE(x) (x) * (x)
Limitations Now:
Module Summary
b = (a + 1) * (a + 1);

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


Pitfalls of macros

Module M06 Continuing ...


#include <iostream>

L
Partha Pratim
Das using namespace std;

E
Weekly Recap #define SQUARE(x) (x) * (x)
Objectives &

T
Outline int main() {
cv-qualifier: int a = 3, b;

P
const &
volatile b = SQUARE(++a);
const

N
Advantages
cout << "Square = " << b << endl;
Pointers
}
C-String
volatile

inline functions Output is 25 in stead of 16 as expected. On the expansion line it gets:


Macros
b = (++a) * (++a);
Pitfalls
inline
Comparison
and a is incremented twice before being used! There is no easy fix.
Limitations

Module Summary

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


inline Function

Module M06

• An inline function is just a function like any other

L
Partha Pratim
Das
• The function prototype is preceded by the keyword inline

E
Weekly Recap

Objectives &
• An inline function is expanded (inlined) at the site of its call and the overhead of

T
Outline
passing parameters between caller and callee (or called) functions is avoided
cv-qualifier:

P
const &
volatile
const

N
Advantages
Pointers
C-String
volatile

inline functions
Macros
Pitfalls
inline
Comparison
Limitations

Module Summary

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


Program 06.04: Macros as inline Functions

Module M06 • Define the function


• Prefix function header with inline

L
Partha Pratim
Das
• Compile function body and function call together

E
Weekly Recap Using macro Using inline
Objectives &

T
Outline #include <iostream> #include <iostream>
cv-qualifier: using namespace std; using namespace std;
inline int SQUARE(int x) { return x * x; }

P
const & #define SQUARE(x) x * x
volatile int main() { int main() {
const int a = 3, b; int a = 3, b;

N
Advantages
b = SQUARE(a); b = SQUARE(a);
Pointers
cout << "Square = " << b << endl; cout << "Square = " << b << endl;
C-String
volatile
} }
inline functions
Macros
Square = 9 Square = 9
Pitfalls
inline
Comparison
Limitations
• 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
Module Summary
• SQUARE(a + 1) fails • SQUARE(a + 1) works
• SQUARE(++a) fails • SQUARE(++a) works
• SQUARE(++a) does not check type • SQUARE(++a) checks type
Programming in Modern C++ Partha Pratim Das M06.22
Macros & inline Functions: Compare and Contrast

Module M06
Macros inline Functions

L
Partha Pratim
Das

• Expanded at the place of calls • Expanded at the place of calls

E
Weekly Recap

Objectives & • Efficient in execution • Efficient in execution

T
Outline
• Code bloats • Code bloats
cv-qualifier:
• Has syntactic and semantic pitfalls • No pitfall

P
const &
volatile
const • Type checking for parameters is not done • Type checking for parameters is robust

N
Advantages
• Helps to write max / swap for all types • Needs template to support all types
Pointers
C-String • Errors are not checked during compilation • Errors are checked during compilation
volatile
• Not available to debugger • Available to debugger in DEBUG build
inline functions
Macros
Pitfalls
inline
Comparison
Limitations

Module Summary

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


Limitations of Function inlineing

Module M06

• inlineing is a directive – compiler may not inline functions with large body

L
Partha Pratim
Das
• inline functions may not be recursive

E
Weekly Recap

Objectives &
• Function body is needed for inlineing at the time of function call. Hence,

T
Outline
implementation hiding is not possible. Implement inline functions in header files
cv-qualifier:
• inline functions must not have two different definitions

P
const &
volatile
const

N
Advantages
Pointers
C-String
volatile

inline functions
Macros
Pitfalls
inline
Comparison
Limitations

Module Summary

Programming in Modern C++ Partha Pratim Das M06.24


Module Summary

Module M06

• Revisited manifest constants from C

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

E
Weekly Recap
interplay with pointers
Objectives &

T
Outline
• Understood the notion and use of volatile data
cv-qualifier:
• Revisited macros with parameters from C

P
const &
volatile
const
• Understood inline functions, their advantages over macros, and their limitations

N
Advantages
Pointers
C-String
volatile

inline functions
Macros
Pitfalls
inline
Comparison
Limitations

Module Summary

Programming in Modern C++ Partha Pratim Das M06.25

You might also like