How Inline and MACRO function are work?
-
Hi All I read some text book .When we write definition of function in .h file.It's automatic a inline function consider by compiler. Though what should be exact size of Inline function.? MACRO:It is parsed by processor. what is difference between Inline and MACRO as speed and execution? Thanks
-
Hi All I read some text book .When we write definition of function in .h file.It's automatic a inline function consider by compiler. Though what should be exact size of Inline function.? MACRO:It is parsed by processor. what is difference between Inline and MACRO as speed and execution? Thanks
inline is now mostly a hint to the compiler; but it decides whatever it wants to do with the code anyway; in general, inline method should be as small as possible and do as little as possible (get/set, simple calculations, ... ) In experience, we kind of stopped using the inline keyword in most new code. Macro are evil. Macros are processed by the preprocessor; it mostly will do a "textual" substitution (very liberal explanation!) of the macro to the actual code and then it will be compiled normally. Macros are not debuggable; meaning you cannot put a breakpoint in it. As for speed, I would say that for simple macro and simple inline methods the compiler and optimizer will make them more or less even. You will have to check for particular test cases in your code to check performance issues. In experience, we stopped using macros (except few particular cases) in new code and try to replace the ones in old code with methods or free functions.
Watched code never compiles.
-
inline is now mostly a hint to the compiler; but it decides whatever it wants to do with the code anyway; in general, inline method should be as small as possible and do as little as possible (get/set, simple calculations, ... ) In experience, we kind of stopped using the inline keyword in most new code. Macro are evil. Macros are processed by the preprocessor; it mostly will do a "textual" substitution (very liberal explanation!) of the macro to the actual code and then it will be compiled normally. Macros are not debuggable; meaning you cannot put a breakpoint in it. As for speed, I would say that for simple macro and simple inline methods the compiler and optimizer will make them more or less even. You will have to check for particular test cases in your code to check performance issues. In experience, we stopped using macros (except few particular cases) in new code and try to replace the ones in old code with methods or free functions.
Watched code never compiles.
-
Though why we use MACRO and INLINE? or what is advantage of it. Is it right way to make general function instead of inline or MACRO?
GAJERA wrote:
Though why we use MACRO and INLINE? or what is advantage of it.
You're really comparing apples to oranges. While you can make the
#define
preprocessor directive look like a function (e.g.,min()
,max()
,HIWORD()
), there's nothing functional about it."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Hi All I read some text book .When we write definition of function in .h file.It's automatic a inline function consider by compiler. Though what should be exact size of Inline function.? MACRO:It is parsed by processor. what is difference between Inline and MACRO as speed and execution? Thanks
GAJERA wrote:
When we write definition of function in .h file.It's automatic a inline function consider by compiler.
This does not make sense, a function definition is merely a hint to the compiler so that it can generate the correct code for a function call in the code. It has nothing to do with either
inline
or macros.GAJERA wrote:
what is difference between Inline and MACRO as speed and execution?
They are totally different beasts so you cannot compare them. The
inline
keyword tells the compiler to generate the code 'inline' rather than via a function call. A MACRO is used to generate (possibly) different source language from parameters in the macro call. The modified source is then compiled to object code and may be inline or include function calls.Just say 'NO' to evaluated arguments for diadic functions! Ash
-
Hi All I read some text book .When we write definition of function in .h file.It's automatic a inline function consider by compiler. Though what should be exact size of Inline function.? MACRO:It is parsed by processor. what is difference between Inline and MACRO as speed and execution? Thanks
GAJERA wrote:
When we write definition of function in .h file.It's automatic a inline function
No it's not - if that's what your book said it was probably written by Herb Schildt and should be consigned to the bin. There are two ways of getting a function expanded inline: - explicitly qualify it's definition with the
inline
keyword - define a function in the body of a class In both cases it's at the discretion of whoever wrote the compiler. If their cost/benefit analysis reckons there's no gain to be had by inlining the function the compiler won't do it. [Likewise there's no reason why a compiler and linker can't conspire to do inlining across modules - VC++ has done that for years...] Personally I only ever use inline functions when writing templates, otherwise I don't bother so the size of the functions that may or may not be inlined is a problem for someone else. Likewise with macros - I only ever use them if there's no other option within the programming language. Inline functions are usually a better bet as they're processed by the compiler and it can give you far more meaningful error messages than the preprocessor does. Execution speed wise it's impossible to say in the general case. Ash -
Hi All I read some text book .When we write definition of function in .h file.It's automatic a inline function consider by compiler. Though what should be exact size of Inline function.? MACRO:It is parsed by processor. what is difference between Inline and MACRO as speed and execution? Thanks
The Macro is dealed with the preprocessor, it is only text replace. Because the C language does not have const keyword. so coder uses the Macro to define the const. And the function defined too. The head file can use #define to define a macro to forbide one head file included more than once in a project. Now the Visual studio 2005 can use #prama once instead of it. The preprocessor cannot check the Macro type. so some error happens because for the Macro. The inline can let function defined inside class. Because one function called, it need to do jump, push stack, pop stack operations. So if some function simplely processes data and is called frequently, you can define this function as inline function to reduce the time used to call method. The incline funciton will be expansion by processor in the function called position. After compiled, you cannot find the inline function name. I use inline function to cout the size of a struct. About the speed, I donot test which is more quickly. But the c++ suggests not to use the macro. It often leads to error.