473,770 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inline recursive functions?

I got this on an interview: Is it possible to write inline recursive
functions?

I said yes, but there is no guarantee that the compiler will
definitely
inline your code even if you write "inline". Is this a right answer?
It seems to be independent of whether or not the function is
recursive.

Another question: how can you tell if the compiler has inlined your
code when you have used "inline"? Compare the size of the binary?

Apr 30 '07 #1
5 4943
Digital Puer wrote:
I got this on an interview: Is it possible to write inline recursive
functions?

I said yes, but there is no guarantee that the compiler will
definitely
inline your code even if you write "inline". Is this a right answer?
It seems to be independent of whether or not the function is
recursive.
I'd say that's correct insofar as the C++ language is concerned. It's
syntactically permitted to declare the function inline, and the compiler
is likewise free to ignore this suggestion.

In practice, I suspect it's unlikely that a compiler would inline a
recursive function call, but it's not impossible. One could even
imagine "hybrid" situations where, for example, the compiler inlines the
initial function call but not any of the subsequent recursive calls.
Another question: how can you tell if the compiler has inlined your
code when you have used "inline"? Compare the size of the binary?
It's all platform dependent here. You could look at the binary, you
could look at the object file, you might find this information from a
debugger, and so on.
May 1 '07 #2
Mark P wrote:
It's
syntactically permitted to declare the function inline, and the compiler
is likewise free to ignore this suggestion.
Actually the compiler cannot ignore the 'inline' definition.
'inline' causes a different type of linking for the function than
it would be without it, especially if the compiler does not inline
the function.
Unlike eg. the 'register' keyword, which is nowadays a no-op,
'inline' isn't a no-op.
May 1 '07 #3
On 1 Maj, 01:40, Digital Puer <digital_p...@h otmail.comwrote :
I got this on an interview: Is it possible to write inline recursive
functions?

I said yes, but there is no guarantee that the compiler will
definitely
inline your code even if you write "inline". Is this a right answer?
Yes. There might be specific compilers where you can force the
compiler to do the inlining, but your answer is the correct generic
one.
It seems to be independent of whether or not the function is
recursive.

Another question: how can you tell if the compiler has inlined your
code when you have used "inline"? Compare the size of the binary?
I would examine the resulting code (e.g. by using the debugger and
looking at the relevant assembler code).

/Peter

May 1 '07 #4
On 1 Maj, 03:44, Juha Nieminen <nos...@thanks. invalidwrote:
Mark P wrote:
It's
syntactically permitted to declare the function inline, and the compiler
is likewise free to ignore this suggestion.

Actually the compiler cannot ignore the 'inline' definition.
'inline' causes a different type of linking for the function than
it would be without it, especially if the compiler does not inline
the function.
Unlike eg. the 'register' keyword, which is nowadays a no-op,
'inline' isn't a no-op.
This is not quite correct. The compiler is not required to inline. The
only requirement is that the compiler does not complain by seeing
multiple (equal) definitions of the inline function.

/Peter

May 1 '07 #5
On Tue, 01 May 2007 00:54:17 +0000, Mark P wrote:
Digital Puer wrote:
>I got this on an interview: Is it possible to write inline recursive
functions?

I said yes, but there is no guarantee that the compiler will definitely
inline your code even if you write "inline". Is this a right answer? It
seems to be independent of whether or not the function is recursive.

I'd say that's correct insofar as the C++ language is concerned. It's
syntactically permitted to declare the function inline, and the compiler
is likewise free to ignore this suggestion.

In practice, I suspect it's unlikely that a compiler would inline a
recursive function call, but it's not impossible.
FWIW, not that unlikely: gcc, for example, will inline recursive
functions (or not, or to some depth) according to what appear to be
rather complex heuristics; it also allows some control of inline
recursion via parameters such as max-inline-recursive-depth.

[...]

--
Lionel B
May 1 '07 #6

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

Similar topics

14
2788
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What am I missing? If that's all, then why did Bjarne even bother adding it to the language? If that's not all, what else can I do with "inline"?
47
3885
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
7
2862
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must be defined either within the class or within the same header file. But its generally a good programming practice to have the declarations and definitions in seperate files. This would make the future maintenance of the code easier.
43
13306
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
7
6132
by: Aloo | last post by:
Dear friends, If we declare a recursive function as 'inline' then does it actually convert to an iterative form during compilation ( the executable code is iterative)? Is it possible ? Please reply.
3
6293
by: Aloo | last post by:
Let's say I have a function func() { ............ func(); .......... }
4
3843
by: Peter Olcott | last post by:
What is the best way to handle this?
7
1879
by: sam_cit | last post by:
Hi Everyone, I have a function declared as inline and i was wondering as to how it would be expanded by the compiler sample(int x) { if (x>0) sample(--x); printf("%d",x); }
1
1466
by: Sylvain Audi | last post by:
I know that declaring a function as inline does not imply the function will actually be inlined (e.g. recursive functions, etc...). In that case, where would the function be instanciated? My guess is that it would be instanciated in the compilation unit that uses it. But in this case, several compilation units that use the same inline function would mean that the linker has to deal with several instances of the same function: shouldn't...
0
9595
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
9432
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
10059
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
10008
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
9873
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
8891
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
6682
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
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.