Module2 Mix
Module2 Mix
// Code
}
Purpose of Attributes in C++Amity School of Engineering & Technology
To give additional information to the compiler for optimization
purposes
Special Case 1 Special Case 2
#include <iostream>
int f(int i)
#include <string>
{
int main()
switch (i) {
{
case 1:
// Set debug mode in compiler or 'R'
[[fallthrough]];
[[maybe_unused]] char mg_brk = 'D';
[[likely]] case 2 : return 1;
// Compiler does not emit any warnings
}
// or error on this unused variable
return -1;
}
}
Inline Functions Amity School of Engineering & Technology
If a function is inline, the compiler places a copy of the code of that function at
each point where the function is called at compile time.
Any change to an inline function could require all clients of the function to be
recompiled because compiler would need to replace all the code once again
otherwise it will continue with old functionality.
To inline a function, place the keyword inline before the function name and
define the function before any calls are made to the function.
The compiler can ignore the inline qualifier in case defined function is more
than a line.
A function definition in a class definition is an inline function definition, even
without the use of the inline specifier.
Inline Functions Amity School of Engineering & Technology
#include <iostream>
Output
using namespace std;
Max (20,10): 20
inline int Max(int x, int y) {
return (x > y)? x : y; }
Max (0,200): 200
The const member functions are the functions which are declared as
constant in the program.
The object called by these functions cannot be modified.
It is recommended to use const keyword so that accidental changes to
object are avoided.
A const member function can be called by any type of object. Non-const
functions can be called by non-const objects only.
syntax
datatype function_name const();
Constant Member functions
Amity School of Engineering & Technology
Output
The value using object d : 28
The value using object d1 : 8
Memory Management System Amity School of Engineering & Technology
The Heap
• Large area of memory controlled by the runtime system that is
used to grant dynamic memory requests.
• It is possible to allocate memory and “lose” the pointer to that
region without freeing it. This is called a memory leak.
• A memory leak can cause the heap to become full
• If an attempt is made to allocate memory from the heap and
there is not enough, an exception is generated (error)
Explicit memroy allocation/ Amity School of Engineering & Technology
Thanks