What are the advantages and disadvantages of using inline and const
Inline:
Advantages:
- It does not require function calling overhead.
- It additionally save overhead of variables push/pop on the stack, while function calling.
- It also save overhead of return call from a function.
- It increases locality of reference by utilizing instruction cache.
- Inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function call outline and return.
Disadvantages:
- If you use too many inline functions then the size of the binary executable file will be large, because of the duplication of same code.
- Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch from that of cache memory to that of primary memory.
- It may cause compilation overhead as if somebody changes code inside inline function than all calling location will also be compiled.
Const:
Advantages:
- If you want any variable to keep constant through out the program then you can use const keyword.
- It is stored in the read only of the initalized data segment , even the pointer storing it’s location cannot change the value of it, pointer can just change the address to point at some other location.
Disadvantages:
- As the const is not flexible so mostly people use #define which is more flexible than const.