473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

inline function vs function-like macro

Suppose I am using a compiler which is C99 compliant.

Function-like macros have the disadvantage if an argument with side-
effects is passed.
For example,

#define SQUARE(x) ( (x) * (x) )

If SQUARE(x++) is called, it results in undefined behaviour.

Instead we can use the C99 feature of inline function. Am I correct ?

Given this, is there any reason for still preferring function-like
macros over inline function feature of C99 ?

Mar 6 '07 #1
2 3260

su**************@yahoo.com, India wrote:
Suppose I am using a compiler which is C99 compliant.

Function-like macros have the disadvantage if an argument with side-
effects is passed.
For example,

#define SQUARE(x) ( (x) * (x) )

If SQUARE(x++) is called, it results in undefined behaviour.

Instead we can use the C99 feature of inline function. Am I correct ?

Given this, is there any reason for still preferring function-like
macros over inline function feature of C99 ?
Yes. Macros allow use without regard to type checking. Because they're
a compile-time feature, they can be used to do certain things that
would not be possible with functions. But more importantly, inline is
a C99 feature and is not very portable across a wide range of
compilers.

Also inline specifies to the compiler to make access to the function
as fast as possible, but doesn't actually require it to embed the code
for the function, wherever it's called, unlike with macros.

Mar 6 '07 #2
su**************@yahoo.com, India wrote, On 06/03/07 03:26:
Suppose I am using a compiler which is C99 compliant.

Function-like macros have the disadvantage if an argument with side-
effects is passed.
For example,

#define SQUARE(x) ( (x) * (x) )

If SQUARE(x++) is called, it results in undefined behaviour.

Instead we can use the C99 feature of inline function. Am I correct ?
Yes, given certain limitations.
Given this, is there any reason for still preferring function-like
macros over inline function feature of C99 ?
Yes, in certain situations. Taking your example, you can pass an int,
double or any other numeric type to SQUARE and it will "do the right
thing"(tm). However, if you use inline functions and you want to be able
to use it on all types without converting ints to doubles, or even a
complex type you will have to write squarei, squarel, squared, squarecd
etc. and call the correct one. Not always an issue, but a point to bare
in mind.
--
Flash Gordon
Mar 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg....
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
18
by: Method Man | last post by:
If I don't care about the size of my executable or compile time, is there any reason why I wouldn't want to inline every function in my code to make the program run more efficient?
4
by: Nish | last post by:
Hi, I have a question regarding inline functions in C. If I declare a function as inline in the header file but do not define it there. Then I define that function in .c file, however neither...
7
by: Wu Shaohua | last post by:
Hi Guys, 1. As we know usually we should not define a constructor as inline. I also learned if we define a member function inside the class this member function will be automatically be...
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
9
by: chinu | last post by:
hi all, i did a small experiment to grasp the advantages of declaring a function as inline. inline int fun1(); int main(){ unsigned int start=0,end=0; asm("rdtsc\n\t"
14
by: jg | last post by:
Does C++ standard require an inline function be generated all the time ? For example, #include <iostream> using namespace std; inline int foo() {
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.