473,836 Members | 1,560 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 8913
In article <12************ *@news.supernew s.com>,
Andrey Tarasevich <an************ **@hotmail.comw rote:
>The previous poster was probably thinking (correctly) that it's not
usefully possible to take the address of a function that has been
inlined. If you take the address of a function declared inline, the
compiler will have to produce an out-of-line version as well.
>Strictly speaking, "has been inlined" is not a property of the
function itself. "Has been inlined" is something that can only be
said about each particular invocation of the function, each
particular call.
Of course I meant "all of whose calls have been inlined".
>It is true that an attempt to take the addresss of an inline function
will make the compiler to generate the out-of-line body for the
function. But I don't see how this can impact the "usefulness " of the
function.
I meant that you could conceivably get the address of the code
corresponding to an inlined call to the function, but that wouldn't be
useful.

-- Richard
Oct 19 '06 #31
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <12************ *@news.supernew s.com>,
Andrey Tarasevich <an************ **@hotmail.comw rote:
[...]
>>It is true that an attempt to take the addresss of an inline function
will make the compiler to generate the out-of-line body for the
function. But I don't see how this can impact the "usefulness " of the
function.

I meant that you could conceivably get the address of the code
corresponding to an inlined call to the function, but that wouldn't be
useful.
No, you couldn't, unless you could use such an address to call the
function with arbitrary arguments.

You can legally take the address of a function (even if it's marked
"inline"), and you can use that address to call that function. That's
what function addresses are for.

Presumably a compiler could use "inline" as a hint that *direct* calls
to the function should be inlined. That doesn't make indirect calls
useless.

For example, I might have a function that computes some mathematical
formula. The formula might be simple enough that the overhead of a
normal function call is significant, so I mark it as "inline". But I
might still want to paas that function's address to, say, a function
that will compute an approximation of its integral over a specified
range. For that, I need its address.

--
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 19 '06 #32
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>I meant that you could conceivably get the address of the code
correspondin g to an inlined call to the function, but that wouldn't be
useful.
>No, you couldn't, unless you could use such an address to call the
function with arbitrary arguments.
I'm clearly not being clear here. There's (probably) an address at
which the inlined code starts, but it would be useless for the
compiler to give you that as the address of the function, because you
couldn't use it to call the function. I didn't mean it would be
possible for a conforming C compiler to do such a thing.

I should just have left out "usefully" in my original comment:

The previous poster was probably thinking (correctly) that it's not
usefully possible to take the address of a function that has been
inlined.

I only put it in because I was expecting some pedant to say "it is
*possible* to take the address, you just can't call it".

-- Richard
Oct 19 '06 #33
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>>I meant that you could conceivably get the address of the code
correspondi ng to an inlined call to the function, but that wouldn't be
useful.
>>No, you couldn't, unless you could use such an address to call the
function with arbitrary arguments.

I'm clearly not being clear here. There's (probably) an address at
which the inlined code starts, but it would be useless for the
compiler to give you that as the address of the function, because you
couldn't use it to call the function. I didn't mean it would be
possible for a conforming C compiler to do such a thing.

I should just have left out "usefully" in my original comment:

The previous poster was probably thinking (correctly) that it's not
usefully possible to take the address of a function that has been
inlined.

I only put it in because I was expecting some pedant to say "it is
*possible* to take the address, you just can't call it".
Ok, I see what you mean -- but I think you were mistaken in your
original assumption.

Here's what Paul Hsieh ("websnarf") wrote:
| 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.)

A couple of followups later, you wrote:
| The previous poster was probably thinking (correctly) that it's not
| usefully possible to take the address of a function that has been
| inlined. If you take the address of a function declared inline, the
| compiler will have to produce an out-of-line version as well.

I'm reasonably sure that Paul *meant* simply that the standard
disallows taking the address of a function to which "inline" has been
applied, just as it does for a variable to which 'register has been
applied. (He was mistaken, though the standard easily *could* have
imposed this restriction, and it arguably would have made some sense.)

Taking the address of an inlined expansion of a function doesn't make
much sense at all, though I suppose it would be theoretically
possible. By anticipating a point that nobody actually made, I'm
afraid you've managed (quite unintentionally , I'm certain) to create
the very confusion you were trying to prevent.

--
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 19 '06 #34

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

Similar topics

13
6578
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
1358
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
13320
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
2664
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
9013
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
3284
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
2640
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
4069
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
9668
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10840
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
10546
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...
1
10588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
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
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
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
3112
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.