difference between macro and inline function in embedded c and how to use it

Spread the love

A macro is a name given to a block of C statements as a preprocessor directive. Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function).

difference between macro and inline function in embedded c

A macro is a name given to a block of C statements as a preprocessor directive. Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function). A macro is defined with the preprocessor directive, #define.When a c program is executed. In RAM one block of memory will store the history of the function call. Whenever a function is called it will be stored in RAM when RAM does not have memory to store new function the last uncalled memory is cleared.
If a function is declared as inline then that function will remain in the RAM so that the allocating memory, removing and reallocating memory will not happen. So that it will reduce the time of execution and increase the processing speed.
At the same time when a function is declared as inline then it will only tell the compiler to tell that to keep it in RAM if it has memory. When RAM does not have memory to allocate then even a inline function will also be removed.
Example:-
-#define A 10
-#define SUM(x,y) ((x) + (y))
Example:-
inline int cube(int s)
{
return sss;
}
int main()
{
int a = 11;
cout << “The cube of 3 is: ” << cube(3) << “n”;
return 0;
}
Advantages :
1.Symbolic constants are evaluated by the PRE-processor,i.e. they are re-written as literal values, not as variables. this has some nice side-effects that you can do using symbolic defines eg. macros and functions inside them.
run-time access to symbolic constants should be significantly faster than access to variables.
opposite to that, variables (and constants) are evaluated by the processor (compiler).
It is easy to modify the value of the constant
variable,since we have to change the value at the macro definition only
Advantages:-
1) It does not require function calling overhead .
2) It also save overhead of variables push on the stack , while function calling .
3) It also save overhead of return call from a function .
4) It increases locality of reference by utilizing instruction cache .
5) After in-lining compiler can also apply intra procedural optmization 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 .
Disadvantage:-
Disadvantage of the macro is the size of the program.The reason is, the pre-processor will replace all the macros in the program by its real definition prior to the compilation process of the program.
Disadvantages :-
1) May increase function size so that it may not fit on the cache , causing lots of chance miss
2) After in-lining function if variables number 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 some body changes code inside inline function than all calling location will also be compiled .
4) If used in header file , it will make your header file size large and may also make it unreadable .
5) If somebody used too many inline function resultant in a larger code size than it may cause thrashing in memory . More and more number of page fault bringing down your program performance .
6) Its not useful for embedded system where large binary size is not preferred at all due to memory size constraints .
difference between macro and inline function in embedded c

Summary :

In this article we saw difference between macro and inline function in embedded c and how to use it so about this section you have any query then free to ask me

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

sachin Pagar

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Reply