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

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 4913
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...@hotmail.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
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...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
7
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...
43
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...
7
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 ? ...
3
by: Aloo | last post by:
Let's say I have a function func() { ............ func(); .......... }
4
by: Peter Olcott | last post by:
What is the best way to handle this?
7
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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
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
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.