473,883 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inline functions vs. macros


When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?

What sort of things is it still reasonable to do using macros? For
example, is it reasonable to write type generic functions macros?

#define CUBE(I) ( (I) * (I) * (I) )

Is there some more concise set of things where inline functions should
be used instead of macros? Multi-statement macros, for example?

Thanks,
rCs
Oct 18 '06
33 8916
Robert Seacord posted:
When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?

Not in general, no, for three reasons:

(1) C90 doesn't support inline.
(2) Even if the compiler _does_ support inline, the code is guaranteed to
be inlined.
(3) It doesn't yield a compile-time constant.

What sort of things is it still reasonable to do using macros? For
example, is it reasonable to write type generic functions macros?

Yes, it's grand.

#define CUBE(I) ( (I) * (I) * (I) )

This one evaluates its argument more than once, but that's OK since the
macro name is in ALL CAPS, which serves as a warning to the programmer.

Is there some more concise set of things where inline functions should
be used instead of macros? Multi-statement macros, for example?

If you need to define variables _and_ return a value, you might need a
function.

--

Frederick Gotham
Oct 18 '06 #11
Darko top-posted [reordered]:
Robert Seacord wrote:
>When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?

Well, as the majority probably know, macros are really tricky to use
and one should be very careful with them. The best example I know when
they shouldn't be used, is the following:

#define max(a,b) ((a)>(b)?(a):(b ))
int x = 4, y = 5;
int z = max(x++, y++);

One would expect the x and y to be incremented once each, but the fact
is the text renders into the following:
int z = ((x++)>(y++)?(x ++):(y++)),
which means the bigger of the two will get incremented twice instead of
only once.
And if you had defined your macro in ALL UPPERCASE, someone writing

int z = MAX(x++ .... hey! it's MAX in all uppercase
.... must be a macro, let me do it like this instead

x++; y++;
int z = MAX(x, y); /* wheew! good thing the original programmer
used ALL UPPERCASE for this macro :) */

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 18 '06 #12
Robert Seacord wrote:
When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?
For the purposes of portability or long term viability/maintainability ,
use of C99 is *not* a reasonable recommendation. You should use C
compilers that will have long term support, or otherwise write code to
the lowest common denominator (portable C/C++.)

Keep in mind that "static" function declarations are usually basically
equivalent to what is intended by C99's "inline". For serious
compilers, there should be no effective difference between the two
(inline further asserts that taking the address of the function is
illegal, however, a static function whose address *isn't* taken (which
it can always determine because it *is* static) becomes equivalent in
functional status.)
What sort of things is it still reasonable to do using macros? For
example, is it reasonable to write type generic functions macros?

#define CUBE(I) ( (I) * (I) * (I) )

Is there some more concise set of things where inline functions should
be used instead of macros?
The general rule of thumb that I use is that complicated macros should
be module local and commented (or obvious). Otherwise, macros should
be limited to constants, or convenience wrappers.
Multi-statement macros, for example?
There's the whole issue of if() else and multi-line macros. The way
you can get around it is to wrap your multi-line macro with a do { ...
} while(0);. Unfortunately many compilers complain that the while (0)
condition is constant and issue a warning. I don't like turning this
warning off in the compiler, because its reasonable if the "0" comes
from some compiler simplifications . So we're kind of in a no-win
scenario here.

Sometimes some kinds of multi-statement macros can lead to interesting
conundrons. Suppose we have the following macro:

#define hf_idxFirst(t,i ) {\
i = hf_NOT_FOUND;\
if (NULL != (t)) hf_idxNext(t,i) ;\
}

This is fine, except that you cannot write code such as this:

for (hf_idxFirst (table, i); !hf_idxIsEnd(ta ble,i);
hf_idxNext(tabl e,i)) {
/* ... */
}

This isn't so much a multi-statement macro issue as much as a control
code fragment limitation issue that gets hidden by a macro.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Oct 18 '06 #13
we******@gmail. com writes:
[...]
Keep in mind that "static" function declarations are usually basically
equivalent to what is intended by C99's "inline". For serious
compilers, there should be no effective difference between the two
(inline further asserts that taking the address of the function is
illegal, however, a static function whose address *isn't* taken (which
it can always determine because it *is* static) becomes equivalent in
functional status.)
I believe it's perfectly legal to take the address of an inline
function. Can you cite something in the standard that says otherwise?
In particular, the function call operator expects a
pointer-to-function, so just calling a function implicitly takes its
address.

(The standard *could* have said that taking the address of an inline
function other than in a direct call is illegal, but it doesn't.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 18 '06 #14
we******@gmail. com wrote:
...
inline further asserts that taking the address of the function is
illegal
...
That's incorrect. There's absolutely nothing illegal in taking the address of an
inline function.

--
Best regards,
Andrey Tarasevich
Oct 18 '06 #15
Frederick Gotham wrote:
Robert Seacord posted:

>>When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?

Not in general, no, for three reasons:

(1) C90 doesn't support inline.
The OP asked about C99.
(2) Even if the compiler _does_ support inline, the code is guaranteed to
be inlined.
(3) It doesn't yield a compile-time constant.
?

--
Ian Collins.
Oct 19 '06 #16
Ian Collins said:
Frederick Gotham wrote:
>Robert Seacord posted:
>>>When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?

Not in general, no, for three reasons:

(1) C90 doesn't support inline.

The OP asked about C99.
Nevertheless, it's a reasonable point. When writing C99 code, it is for the
time being (and, the way, we're going, for the foreseeable future too)
reasonable to keep the code portable to C90, unless one is prepared to
forego the advantages thereof.
>(2) Even if the compiler _does_ support inline, the code is guaranteed to
be inlined.
(3) It doesn't yield a compile-time constant.
?
I'm not sure what he means by that, either.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 19 '06 #17
Andrew Poelstra said:
On Wed, 2006-10-18 at 21:16 +0000, Frederick Gotham wrote:
>Robert Seacord posted:
When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?
Not in general, no, for three reasons:

(1) C90 doesn't support inline.
(2) Even if the compiler _does_ support inline, the code is guaranteed to
be inlined.

You meant /not/ guaranteed to be inline. According to C99, that is.
I am fairly sure he meant something like "[the macro version of] the code is
*guaranteed* to be inlined".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 19 '06 #18
Richard Heathfield wrote:
Ian Collins said:

>>Frederick Gotham wrote:
>>>Robert Seacord posted:
When writing C99 code is a reasonable recommendation to use inline
functions instead of macros?

Not in general, no, for three reasons:

(1) C90 doesn't support inline.

The OP asked about C99.


Nevertheless, it's a reasonable point. When writing C99 code, it is for the
time being (and, the way, we're going, for the foreseeable future too)
reasonable to keep the code portable to C90, unless one is prepared to
forego the advantages thereof.
True, which brings us back to the old dilemma of pragmatism or
portability. I think every recent (last 10 years) C compiler I've used
either supports inline as an extension, or inlines as an optimisation,
or both.
--
Ian Collins.
Oct 19 '06 #19
Ian Collins said:
Richard Heathfield wrote:
[...]
>When writing C99 code, it is for
the time being (and, the way, we're going, for the foreseeable future
too) reasonable to keep the code portable to C90, unless one is prepared
to forego the advantages thereof.
True, which brings us back to the old dilemma of pragmatism or
portability.
For me, portability is part of pragmatism.

And so, of course, is non-portability.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 19 '06 #20

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

Similar topics

13
6580
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. accessor and mutator methods to access private data members). I would like to know if there are any issues with using inline functions that may have attributed to my errors before I start separting them out into "outline functions". Regards
3
1362
by: Peng Yu | last post by:
Hi, I'm trying to define inline_test::test() to be inline. But I always got errors. I know that if I define inline_test::test() in inline_test.h, there are no errors. But I still would rather to put the member functions in *.cc files not the *.h files. Do you know how to do that? The following paragraphs are the compile error and the source codes.
43
13330
by: Patrick Laurent | last post by:
Hello I have a program with many many inlined template functions It is essential for the execution speed that every (or almost every) function marked as inlined, becomes really inlined by the compiler. I already compiled the program with Intel Compiler (ICL) on Visual C++, and it works fine and fast. I verified that the functions are really inlined. But with GCC 3.4 (Linux+Cygwin) or ICC (Linux), The same program is about 5
9
2668
by: Bilgehan.Balban | last post by:
Hi, If I define an inline function in one .c file, and use it from another, after compiling and linking the two, it seems the function is not inlined but rather called as a regular function. I would expect to see it inlined during linkage of the two object files. Does inlining only occur if the inline function is defined within the same file that it is called? Thanks,
12
676
by: sam_cit | last post by:
Hi Everyone, I have few questions on inline functions, when i declare a function as inline, is it for sure that the compiler would replace the function call with the actual body of the function? or is it a call taken by compiler? Second, i see that it is same as what Macro's used to do for c, if so what is the advantage for going in for inline functions than to Macros?
37
9019
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to inline function ? thanks for any help in advance ...
2
3286
by: subramanian100in | last post by:
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.
15
2645
by: Mahesh | last post by:
Hi, I need to know if stack frames are generated in case of a inline function execution or do they execute just like macros? if they execute like macros, then what is the need for having inline function? where would you use macros and where inlines? Thanks Mahesh
6
4073
by: Laurent Deniau | last post by:
When I compile the code below with gcc -std=c99 -W -Wall -pedantic -O3 -Winline, it reports the following: variadic.c: In function ‘fv’: variadic.c:12: warning: function ‘fv’ can never be inlined because it uses variable argument lists variadic.c: In function ‘vf’: variadic.c:12: warning: inlining failed in call to ‘fv’: function not inlinable variadic.c:27: warning: called from here
0
9933
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11121
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10407
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5793
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.