Professional Documents
Culture Documents
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
Module 06
inline
functions
Macros
inline
Summary
inline
functions
Macros
inline
Summary
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
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
inline
functions
Macros
inline
Summary
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
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
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
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
inline
functions
Macros
inline
Summary
Module 06
Summary
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
}
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
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;
Module 06
inline
functions
Macros
inline
Summary
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
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
Module 06
Summary
Module 06
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