473,394 Members | 1,769 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.

regarding recursive function declared as inline

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);
}

Does anyone have any idea?

Dec 12 '06 #1
7 1848
sa*****@yahoo.co.in wrote:
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);
}

Does anyone have any idea?
Yes. Compile it and see.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 12 '06 #2
sam_...@yahoo.co.in wrote:
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);
}

Does anyone have any idea?
There is no requirement that the compiler expand inline everything
declared inline -- the keyword is only a hint. Recursion is probably
one of the things that could make the compiler skip it, but you'd have
to check the assembly output of your specific compiler to find out for
sure. If it was smart enough to expand the function above, it would
probably just turn it into a loop.

Cheers! --M

Dec 12 '06 #3
>
Yes. Compile it and see.
Yes i did and it did work, but i'm afraid the expansion of inline
functions are not to be seen, in my case i made sure that the input to
the recursive function was determined at run time as an input, so how
does compiler expand this case? or is it that the compiler leaves the
function as it is and is invoking it as an ordinary function call?

Dec 12 '06 #4
There is no requirement that the compiler expand inline everything
declared inline -- the keyword is only a hint. Recursion is probably
one of the things that could make the compiler skip it, but you'd have
to check the assembly output of your specific compiler to find out for
sure. If it was smart enough to expand the function above, it would
probably just turn it into a loop.
Thanks for the response, now i understand that inline keyword is just
a request for the c++ compiler to do something different,, however do
you think every recursive logic can be expressed in a loop (even using
some extra variables)?

Dec 12 '06 #5
sa*****@yahoo.co.in wrote:
however do
you think every recursive logic can be expressed in a loop (even using
some extra variables)?
Yes, anything that can be expressed recursively in C++ can be, not
least because all C++ code is ultimately translated into some machine's
instructions, and most machines have support only for the equivalent of
goto statements, which are used for functions and loops.

Cheers! --M

Dec 12 '06 #6
sa*****@yahoo.co.in wrote:
>Yes. Compile it and see.

Yes i did and it did work, but i'm afraid the expansion of inline
functions are not to be seen, in my case i made sure that the input to
the recursive function was determined at run time as an input, so how
does compiler expand this case? or is it that the compiler leaves the
function as it is and is invoking it as an ordinary function call?
Compile it, make sure your compiler outputs the assembly, and see.

There is no way to tell whether a function has been inlined or not, by
means of C++ language. It's not what's known as "observable behaviour".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 12 '06 #7

sa*****@yahoo.co.in wrote:
There is no requirement that the compiler expand inline everything
declared inline -- the keyword is only a hint. Recursion is probably
one of the things that could make the compiler skip it, but you'd have
to check the assembly output of your specific compiler to find out for
sure. If it was smart enough to expand the function above, it would
probably just turn it into a loop.

Thanks for the response, now i understand that inline keyword is just
a request for the c++ compiler to do something different,, however do
you think every recursive logic can be expressed in a loop (even using
some extra variables)?
You need a potentially unbounded number of variables or pointers and an
unbounded memory space (i.e. a stack, which recursion gives you for
free), but if you have that, it can be done.

In general the compiler won't be kind enough to do that sort of
optimization for you though, but there's one case in which most will
(at least with -O2 or whatever) called tail recursion.

You can look up the details, but if you wanted to compute, say,
factorials by recursion instead of loops (you're coming from ML or Lisp
or something) the naive approach is:
int fact(int n) {
return n * fact(n-1);
}
(I'm ignoring the fact that this won't work for n larger than some
smallish number). But this is a poor way of writing it; much better is
int fact(int n, int m) {
if(n == 0) return m;
else return fact(n-1, n*m);
}

int fact(int n) {
return fact(n, 1);
}
because the compiler will probably optimize that recursion into just a
loop, saving you both function call overhead in terms of time and
(usually more importantly) stack space.

Evan

Dec 12 '06 #8

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

Similar topics

6
by: kceiw | last post by:
How to make a member function inline while the declaration and implementation are separated?
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 ? ...
6
by: Sharon | last post by:
Usually it is common to write the class member function in the class H file, but some people like to write the function body in the C++ file. Can anybody tell me what are the cases where inline...
7
by: ypjofficial | last post by:
Hello All, Inline before a function definition is just a request to the compiler to make the function inline. The compiler may or maynot make it inline.. My question is ..is there any way by...
5
by: Digital Puer | last post by:
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...
14
by: jg | last post by:
Does C++ standard require an inline function be generated all the time ? For example, #include <iostream> using namespace std; inline int foo() {
5
by: ciccio | last post by:
Hi, I have a problem with my code that the compiler does not find any inline functions which are static. The simple code example is written below, this is what the compiler throws at me. ]...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
5
by: V | last post by:
Hello: Consider the following recursive function: inline u64 multiplyPower(u64 V, u8 i, u64 c) { if (i == 0) return V; else return mul( multiplyPower(V,i-1,c) , c);
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...
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
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
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,...
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.