answersLogoWhite

0


Best Answer
"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

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Pre-processor directives are processed before compilation begins. This strips out all the directives and rewrites the files according to those directives to produce temporary, pre-processed files or intermediate source files. The compiler then compiles these intermediate source files to produce object files which can then be linked to produce the final machine code executable.

Inline functions are processed during compilation. The compiler's optimisers will first determine if the function is suitable for inline expansion. If so, every call to the function is replaced with inline-expanded code, eliminating the function call altogether and thus speeding up execution of the function code. However, if the function is called from more than one place within your code, then inline expansion will increase the overall code size. If the compiler's optimisers determine that the increased code size would adversely affect performance, then the request for expansion will be ignored. Each inline function is processed separately for optimal performance and declaring a function inline is not a guarantee for inline expansion. However, inline expansion can be forced upon the compiler and, although this is not recommended (the compiler's optimisers are way more intelligent than you), certain functions cannot be forced, such as recursive functions where the depth of recursion cannot be determined at compile time.

Pre-processor directives that define macros as functions are always inline expanded before compilation. While this can be a useful feature, it is thuggish programming that completely bypasses the compiler's optimisers and can result in reduced performance due to increased code size. However, many macro functions can be implemented as function templates which can be inline expanded if the optimiser determines there is benefit in doing so. The compiler's optimisers are there to help you so it pays to enlist them whenever possible. Another advantage is that function templates are much easier to debug; macro functions are inline expanded before compilation, so any errors will only be detected within the inline expanded code, not within the macro (which doesn't exist as far as the compiler is concerned because it was stripped out during pre-processing). This also means you cannot set breakpoints in macros. However, the biggest problem with macros is that they are not type safe, and if misused, can lead to unwanted side-effects at runtime, which is never a good thing. Again, function templates avoid these problems because they are an intrinsic part of the language, are completely type safe, and the compiler will help you eliminate bugs at compile time, which is always the best place to deal with bugs.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Preprocessor directives are commands that are processed immediately prior to compilation proper. They effectively alter your source code, through macro expansion, to produce intermediate, pre-compiled source files which are then passed to the compiler for final compilation. Your compiler may include an option that allows you to save and view these pre-compiled files.

A function is an actual code block that is defined by or included in your code. Functions allow you to organise code into small subroutines which can be used repeatedly without having to rewrite the code itself.

In other words, functions are the methods used by your program, while preprocessor directives are used solely by the compiler to craft your program.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Greatly. In-line functions can be treated as normal functions except for one aspect: they don't have address, so they cannot be used as call-back functions.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does an inline function differ from a preprocessor macro in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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 are compiler directives?

Compiler directives instruct the preprocessor to perform certain tasks prior to compiling the source code. The language compiler can only compile C code, but macros (lines beginning with a # symbol) are not part of the C language, so these need to be processed before the compiler can do its job. The most common compiler directive is the #include directive, which instructs the preprocessor to import the named file, effectively copy/pasting the content of that file in place of the directive. The content may itself contain directives and these must also be processed accordingly before the content is inserted. Note that the insertion takes place in a temporary file; the source file is not changed by the preprocessor. The temporary file is usually deleted after compilation is complete, however you can configure the compiler to retain these files so you can see what code is produced by the preprocessor and thus what the compiler "sees". The #define and #undefine compiler directives define or undefine macros; symbols which the preprocessor can refer to while processing other directives. Macros can also be defined via the command line. The #ifdef, #ifndef, #elif, #else and #endif compiler directives are used in conditional compilation in conjunction with macro definitions. These tell the preprocessor which parts of the source code to include in or exclude from the temporary file. This is useful when writing debug code that we wish to include in a debug build but exclude from a retail build, or we wish to cater for different architectures and require different libraries and function calls in order to cater for them. A macro symbol can also be given a value. Wherever that symbol appears in the code, the symbol is replaced with the value. This provides a convenient text-replacement system that can help make code easier to read. However, a macro can also be used to define a function (a macro function). These differ from ordinary functions in that they are always inline expanded, however they are not type-safe and the compiler cannot help you debug them (the compiler never sees the macro -- it only sees the inline expanded code produced by the macro). Macro functions are best avoided, however when used with care they can help to resolve complex problems that cannot easily be resolved within the language itself. It's best to think of macro functions as being code generators but, because they are not part of the language, its best to keep their usage as simple as possible. As well as processing compiler directives, the preprocessor also strips out all the user-comments from the source file. Even if a compiler could read a comment it certainly wouldn't understand it, so to keep the compiler implementation as simple and as efficient as possible, all non-C code is completely stripped out. Even the redundant whitespace and newline characters are stripped out. The end result is a compact source file which can be easily compiled -- assuming no errors have been generated by your macros!