Q:
What is the advantage and disadvantage of the inline function?
belongs to collection: C++ Interview Questions and Answers(2022)
C++ Interview Questions and Answers(2022)
- Define C++?
- What is the difference between C and C++?
- What is a class in C++?
- What is an object in c++?
- Why use access modifiers in C++?
- What are C++ access modifiers?
- What are the differences between a class and a structure in C++?
- Why is the size of an empty class not zero in C++?
- What is a constructor in c++?
- Is the default constructor exists in C++?
- What are the various OOPs concepts in C++?
- What is polymorphism in C++?
- What are the different types of polymorphism in C++?
- Compare compile-time polymorphism and Run-time polymorphism in c++?
- What is encapsulation in c++?
- What Is Inheritance in c++?
- What is an abstraction in C++?
- What is a reference in C++?
- What is the default constructor in c++?
- What is a destructor in C++?
- When is the destructor called in c++?
- Is it possible to overload the destructor of the class in c++?
- Should I explicitly call a destructor on a local variable in c++?
- How destructors are different from a normal member function in c++
- What is the difference between constructor and destructor in c++?
- What is “this” pointer in c++?
- Where we should use this pointer in C++?
- What is a “new” keyword in C++?
- What is the difference between new and malloc in c++?
- What is the difference between delete and free in c++?
- What do you mean by call by value and call by reference in c++?
- What is a namespace in c++?
- How to use namespace in C++?
- What is a member function in C++?
- What are static members in C++?
- What do you mean by inline function and How to implement the inline function in C++?
- What is the use of the inline function in C++?
- What is the advantage and disadvantage of the inline function?
- What’s the difference between static, inline, and void with functions?
- What is function overloading in C++?
- Explain some ways of doing function overloading in C++?
- What is operator overloading?
- What is the difference between function overloading and Operator Overloading?
- What is the assignment operator in C++?
- Can you overload a function based only on whether a parameter is a value or a reference in c++?
- What is Overriding in c++?
- Write a C++ program that describes function Overriding?
- What is the difference between function overloading and Overriding in c++?
- How to create and use a reference variable in C++?
- What is the difference between a pointer and a reference in c++?
- What is the virtual function?
- Write some important rules associated with virtual functions?
- Name the Operators that cannot be Overloaded in c++
- Figure out functions that cannot be overloaded in C++?
Answer:
There are a few important advantages and disadvantages of the inline function.
Advantages:-
1) It saves the function calling overhead.
2) It also saves the overhead of variables push/pop on the stack, while function calling.
3) It also saves the overhead of return call from a function.
4) It increases the locality of reference by utilizing the instruction cache.
5) After the inlining compiler can also apply intraprocedural optimization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination, etc..
Disadvantages:-
1) May increase function size so that it may not fit in the cache, causing lots of cache miss.
need an explanation for this answer? contact us directly to get an explanation for this answer2) After inlining function, if variables numbers which are going to use register increases than they may create overhead on register variable resource utilization.
3) It may cause compilation overhead as if somebody changes code inside an inline function then all calling locations will also be compiled.
4) If used in the header file, it will make your header file size large and may also make it unreadable.
5) If somebody used too many inline functions resultant in a larger code size than it may cause thrashing in memory. More and number of page faults bringing down your program performance.
6) It’s not useful for an embedded system where large binary size is not preferred at all due to memory size constraints.