473,785 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1879
sa*****@yahoo.c o.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.c o.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.c o.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.c o.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.c o.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
2411
by: kceiw | last post by:
How to make a member function inline while the declaration and implementation are separated?
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.
6
2109
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 function should not be written in the C++ file? Or what are the disadvantages of inline function body in a C++ file? -- Regards Sharon G.
7
3172
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 which I can find at runtime whether the particular function which is marked as inline,is made inline or is treated like other function by the compiler ?
5
4944
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 "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
14
2334
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
5061
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. ] $ g++ main.cpp foo.cpp /home/klaas/tmp/cciAcYgl.o: In function `main':
6
5931
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 or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
5
2578
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
9645
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
10341
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
10155
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
10095
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,...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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
2881
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.