answersLogoWhite

0

"Differences between inline functions and non"Non-inline functions are your standard functions. When they're called, the compiler puts all the variables and its current position on the stack, jumps to the location in memory where the function resides, executes the instructions, and jumps back to where it was originally called. All that work takes a lot more time then if you had just not used functions. By writing the code directly into your procedure and not using functions you are inlining your code. If you have that same piece of code in multiple places, maintaining your code will become a problem because your code will be duplicated everywhere (because you're not using functions in order to achieve a speed increase).

In order to get the best of both worlds, C++ introduced the inline keyword. By specifying that a function is inline, the compiler will take the function you have written and basically replace the function call that would have been generated with the function itself. In other words, using an inline function is pretty much directly putting your code there, except it's prettier and more maintainable.

Overuse of inlining function will cause bloated code for a very marginal increase in speed, if any. Inline functions are best suited for small quick functions. Also note that the inline keyword is a request to inline a function and the compiler may not necessarily honor that request. One example would be attempting to inline a recursive function.

In other languages like Java and C#, you are not able to specify what is inlined and what is not. The compiler will automatically make that decision without any input from the programmer.

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

Is macro a recursive function?

If you're asking if the c preprocessor supports recursive macros, the answer is no. The preprocessor is single-pass and since the "function" must be defined before it can be referenced, it can not be recursive.


What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point. Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself. Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.


What is a macro in c plus plus?

A macro is preprocessor definition that is processed prior to compilation. All occurances of a macro within your C++ source code are replaced with the macro definition, much like an automated search-and-replace-all operation. If the macro's definition is a function with one or more arguments, then the function is inline expanded within your code, replacing the defined arguments with the arguments that were passed to the macro. However, macro functions are not type safe and cannot be debugged because they only exist in your source code; the compiler only sees the intermediate code emitted by the preprocessor, at which point all macro definitions will no longer exist. To address this problem, C++ also supports the concept of template functions, which not only eliminates any unwanted inline expansion (resulting in smaller code), but also ensures that all calls to the function are type safe and can be debugged in the normal way. That said, macro functions, when used appropriately, can greatly simplify your code and can achieve things that would be difficult if not impossible to achieve with C++ alone. The ability to use code fragments via a macro is one such possibility. However, when combined with preprocessor directives such as #ifdef DEBUG, macros can also be used to provide useful and powerful debugging routines that only exist in debug code, compiling to no code in release builds. This cannot be achieved with C++ alone.


What are preprocessor directives in c programing language?

Preprocessor directives are instructions to the preprocessor which modify the source code prior to compilation. The compiler never sees the directives, it only sees the modified source code. Preprocessor directives can be used to insert the contents of one file into another (#include), define or undefine a macro (#define, #undef), provide conditional compilation (#if, #ifdef, #ifndef, #else, #endif) or provide some implementation-defined operation (#pragma). When the preprocessor acts upon a directive, the directive is not included in the modified file. Where a directive defines a macro, all occurrences of the macro name within the source code are expanded according to the definition. Given that the compiler never sees that definition, this can lead to some obscure error messages where macro expansion results in a compile-time error.


What is recursive macro expansion?

This may seem like a silly question, but you will gain some knowledge on the inner working of the preprocessor of the recursive macro expansion. It will actually require a lot of scripts.

Related Questions

Is macro a recursive function?

If you're asking if the c preprocessor supports recursive macros, the answer is no. The preprocessor is single-pass and since the "function" must be defined before it can be referenced, it can not be recursive.


What are the function of a processor?

The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).


How do you override a defined micro?

By using "#undef" preprocessor we can override a defined macro.


What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point. Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself. Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.


What is a macro in c plus plus?

A macro is preprocessor definition that is processed prior to compilation. All occurances of a macro within your C++ source code are replaced with the macro definition, much like an automated search-and-replace-all operation. If the macro's definition is a function with one or more arguments, then the function is inline expanded within your code, replacing the defined arguments with the arguments that were passed to the macro. However, macro functions are not type safe and cannot be debugged because they only exist in your source code; the compiler only sees the intermediate code emitted by the preprocessor, at which point all macro definitions will no longer exist. To address this problem, C++ also supports the concept of template functions, which not only eliminates any unwanted inline expansion (resulting in smaller code), but also ensures that all calls to the function are type safe and can be debugged in the normal way. That said, macro functions, when used appropriately, can greatly simplify your code and can achieve things that would be difficult if not impossible to achieve with C++ alone. The ability to use code fragments via a macro is one such possibility. However, when combined with preprocessor directives such as #ifdef DEBUG, macros can also be used to provide useful and powerful debugging routines that only exist in debug code, compiling to no code in release builds. This cannot be achieved with C++ alone.


What are preprocessor directives in c programing language?

Preprocessor directives are instructions to the preprocessor which modify the source code prior to compilation. The compiler never sees the directives, it only sees the modified source code. Preprocessor directives can be used to insert the contents of one file into another (#include), define or undefine a macro (#define, #undef), provide conditional compilation (#if, #ifdef, #ifndef, #else, #endif) or provide some implementation-defined operation (#pragma). When the preprocessor acts upon a directive, the directive is not included in the modified file. Where a directive defines a macro, all occurrences of the macro name within the source code are expanded according to the definition. Given that the compiler never sees that definition, this can lead to some obscure error messages where macro expansion results in a compile-time error.


Write a preprocessor directive to accomplish define macro square volume that computes the volume of a square the macro takes one argument?

Pick one: #define SQUARE_AREA(A) ((A) * (A)) #define CUBE_VOLUME(A) ((A) * (A) * (A))


What is recursive macro expansion?

This may seem like a silly question, but you will gain some knowledge on the inner working of the preprocessor of the recursive macro expansion. It will actually require a lot of scripts.


In embedded systems perspective which is better to use. Macro or function?

In VBA, a macro is the name for the function that you can see on the GUI and use, and a function is a procedure called by a macro. A Macro is more "on the stage", and a Function is more "backstage".


What are the functions of a c processor?

The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).


How does macro economics differ from micro economics?

Macro economic is differ from micro economic because macro economic study as a whole economics but micro economic study only of an individual.


What is inline function what are the advantages of using inline function?

Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say: int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro: #define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like: max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like: max(a++, b++) An alternative in C++ is to use inline functions: inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type. Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say: int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro: #define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like: max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like: max(a++, b++) An alternative in C++ is to use inline functions: inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type.