Macro vs Inline Function

The more preferred way of defining these constants is by using the keyword const.

The #define directive is quite powerful and hence allows the macro name to have arguments and thus behave like a function

Inline functions are an imperative feature of C++ and are frequently used with classes. These are no different from the normal functions except for the fact that they are never actually called. These functions are, as their name suggests, expanded in line at each point of invocation. All that needs to be done to enable or cause a function to expand at the point of invocation is to add the inline keyword before the function definition

The function-like macros are pre-processed at compile time, and as a result, there is really no macro existing at runtime. This is why there is an increase in the size of the binary files.

major setback of the function-like macros is the lack of type checking leading to a high level of type insecurity. The arguments passed on to a macro are never preprocessed.

Consider the example below which depicts the disadvantage of the lack of type checking. In this example, the result is always the value passed in as the second argument.

#include "iostream.h"

#define MAX(a, b)              ((a < b) ? b : a)

int main( void)
{
    cout << "Maximum of 10 and 20 is " << MAX("20", "10") << endl;
    return 0;
}
Output:
Maximum of 10 and 20 is 10

Function-like macros when combined with conditional statements need to be very carefully handled. Take a look at the example below:

#include “iostream.h”

#define MAX(a, b) \
if (a < b) \
cout << “Maximum is b:” << b << endl;

int main( void)
{
if (true)
MAX(20, 10)
else
cout << “Macro failed.” << endl;
return 0;
}

Output:
Macro failed.

This will always give “Macro failed.”. This is because the compiler will look at the code as shown below:

#include “iostream.h”

int main( void)
{
if (true)
if (20 < 10)
cout << “Maximum is b:” << b << endl;
else
cout << “Macro failed.” << endl;
return 0;
}

A similar combination which could tamper the result is that of the parenthesis used within the function-like macros. when we remove the enclosing braces of the statement, the output of the macro goes completely berserk. It is always going to give the result of the conditional statement.

#include “iostream.h”

#define MAX(a, b) ((a < b) ? b : a)

int main( void)
{
cout << “Maximum of 10 and 20 is ” << MAX(10, 20) << endl;
return 0;
}

Output:
Maximum of 10 and 20 is 1

Function-like macros need additional handling of arguments that have side effects. Expressions supplied as arguments may not always be evaluated before entering the body of the function-like macros
The point where inline functions primarily score over the function-like macros is perhaps the feasibility to step through the code. This perhaps is the one single motivation that causes developers to be biased if at all towards inline functions.

Inline functions when used effectively increase the performance but then nothing comes for free. The enhanced performance will be at the cost of increased compile time and possibly size. Inline is nothing but a request to the compiler. The onus shifts to the compiler which decides on the optimization once the request has been issued. The compiler would then decide upon the optimization. So, the focus when using inline functions should be on the cost and the benefits, since these are directly coupled with the expansion.

Any function whose body is defined within the class body will be implicitly treated as inline. But there is a catch to this. The implicit treatment depends on the complexity and length of the function. Remember who is the boss when it comes to inline functions? Inline is just a request and it is the compiler who is vested with the authority to accept or reject the inline request.

In the ensuing example, there are two functions of which one has been explicitly declared as inline while the other isn’t. In this case, both are treated by the compiler as inline since their definition is within the class body.

class CInlinesExamples
{
public:
void ImplicitInline()
{ cout << “This is an implicit inline function.” << endl; }

inline void ExplicitlyInline()
{ cout << “This is an explicit inline function.” << endl; }
};

Any function will have to be declared explicitly as inline when the following style is used while writing the code:

class CInlinesExamples
{
public:
inline void InlineFunction();
};

void CInlinesExamples:: InlineFunction()
{
cout << ” This is an explicit inline function.” << endl;
}

The inline directive will be totally of no use when used for functions which are:

* recursive
* long
* composed of loops

In a class, generally the constructor, copy constructor, destructor, and assignment operator overloading are all inline by default.

Inline functions are always on the verge. They might be good or bad, really bad..

Inline functions might make the process larger and/or slower. A process with a number of inline functions will end up having a large code size. This might cause “thrashing” – a computer activity that makes little or no progress, as a result of memory or other resources becoming exhausted or too limited to perform needed operations – on demand-paged virtual-memory systems. To put it in other words, an increase in the executable size may result in more disk operation as the system will end up spending most of its time fetching the next chunk of code from the disk.

inline functions while leading to thrashing may reduce the page faults. The working set size – number of pages that need to be in memory at once – might go down even if the executable size goes up.

class CInlinesExamples
{
public:
void OuterFunction()
{ cout << “This function is the caller function.” << endl; }

void InnerFunction()
{ cout << “This function is the called function.” << endl; }
};

When the InnerFunction is called within the OuterFunction, the code is often on two distinct pages; when the compiler procedurally integrates the code of the InnerFunction into the OuterFunction, the code is often on the same page. This will reduce the possibility of any page faults.

Advertisements

6 Responses to “Macro vs Inline Function”

  1. Bagavathy Raj Says:

    Awesome analysis. Really appreciate it.

  2. Bagavathy Says:

    http://www.codeproject.com/KB/cpp/Macros_vs_Inlines.aspx

  3. am Says:

    u’ve copied this totally off this site. Atleast give credit
    http://www.codeproject.com/KB/cpp/Macros_vs_Inlines.aspx

  4. travel Says:

    vuelos…

    […]Macro vs Inline Function « TheWarrior[…]…

  5. Jamie Says:

    Valuable information. Lucky me I discovered your website
    accidentally, and I’m surprised why this twist of fate did not happened earlier! I bookmarked it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: