473,395 Members | 1,471 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,395 software developers and data experts.

Function inlining in C (not C99)

What's the best way to allow the compiler to inline a function in C? (Not
C99, which already supports the 'inline' keyword.)

The way I do it, is to define the functions in question as 'static' in a
header file:

static void foo()
{
/* ... */
}

Almost every compiler these days will inline the function when compiling
with optimizations turned on (GCC does, I think). Anyway, can something go
wrong with this approach? And if yes, is there a better way to achieve
this?
--
Niko <realnc--(AT)--lycos--(DOT)--de>
http://members.lycos.co.uk/realnc
Nov 13 '05 #1
5 1919
>>>>> "Nikos" == Nikos Chantziaras <fo******@manager.de> writes:

Nikos> What's the best way to allow the compiler to inline a function in C? (Not
Nikos> C99, which already supports the 'inline' keyword.)

The best way would be found by reading the documentation that
accompanies your compiler. It may be something a simple as
-finline-functions or not. I do not believe there is a portable
answer to your question.

Nikos> The way I do it, is to define the functions in question as 'static' in a
Nikos> header file:

This MAY make your functions have more potential to be inlined by your
compiler, but it may not. It depends on your compiler. The static
keyword for functions has other ramifications. To net this out, make
functions static where they should/can be static, but not because they
will be more likely to be inlined.

Nikos> Almost every compiler these days will inline the function when compiling
Nikos> with optimizations turned on (GCC does, I think). Anyway, can something go
Nikos> wrong with this approach?

Yes. If there were no trade-offs why would it be an option? There
are other ways to improve the performance of your code. Maybe you
need a better algorithm vs. inlining... There are many other ways to
improve performance where it is needed.

--
-MM
I rarely read email from this address /"\
because of spam. \ / ASCII Ribbon Campaign
I MAY see it if you put #NOTSPAM# X Against HTML Mail
in the subject line. / \
Nov 13 '05 #2
In <bh************@ID-151409.news.uni-berlin.de> "Nikos Chantziaras" <fo******@manager.de> writes:
What's the best way to allow the compiler to inline a function in C? (Not
C99, which already supports the 'inline' keyword.)

The way I do it, is to define the functions in question as 'static' in a
header file:

static void foo()
{
/* ... */
}

Almost every compiler these days will inline the function when compiling
with optimizations turned on (GCC does, I think). Anyway, can something go
wrong with this approach? And if yes, is there a better way to achieve
this?


The only reliable way of achieving what you want is to use macros instead
of functions (where possible).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3

"Nikos Chantziaras" <fo******@manager.de> wrote in message

What's the best way to allow the compiler to inline a function in C?
(Not C99, which already supports the 'inline' keyword.)

Compile under C++.

Nov 13 '05 #4
Malcolm wrote:

"Nikos Chantziaras" <fo******@manager.de> wrote in message

What's the best way to allow the compiler to inline a function in C?
(Not C99, which already supports the 'inline' keyword.)

Compile under C++.


If you're prepared for your code to fail to compile, or to fail at
runtime, that might work.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #5

"Don Porges" <po****@comcast.net> wrote in message
[ inline by declaring static in header ]
On downside is that if you port to a compiler that doesn't do the
inlining, you end up with a copy of foo()'s code in every .c module that
includes that .h. Multiply by the number of functions you've done this > to.

Since you expect the function to be inlined it can't be too big.
Anyway, code size is seldom much of a problem these days.
Nov 13 '05 #6

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

Similar topics

6
by: glen_stark | last post by:
Hi. I'm just curious if there any warnings or caveats or whatever to be aware of when inlining function object calls? If the answer is no, they inline just like everything else, that's good...
8
by: Mat Booth | last post by:
If I call an inline member function through a member function pointer, will that function be inlined? I imagine it wouldn't because the function that's pointed to isn't known until run-time, is...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
4
by: thinktwice | last post by:
i have just made a test project :(win32 console) //file : func.h #ifndef _FUNC_H_ #define _FUNC_H_ void func1() { return; };
12
by: Tomás | last post by:
The common persuasion is: Big function -- leave it outline. Small function -- make it inline. In code I'm writing at the moment, I have a fairly long function, so it wouldn't cross my...
7
by: Ancient_Hacker | last post by:
In days of old, for no discernible reason, many CPU's had the ability to execute one or more instructions out of registers. Not very common today. Which brings up a semi interesting point. ...
8
by: Yakov | last post by:
I'd like a tool that performed inlining of function bodies of which do not appear in the .h file. Really. gcc on Linux. Yakov
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
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...

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.