Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Inline code

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
0
0
GB
Hi,

What is the benefit of using inline code. Does it make it run faster ?

Will the executable be larger ?

Can i use inline functions in all my classes ?

Thanks.

Rich.
 
Well, the main reason is efficiency. Every time a function is called, a series of instructions must be executed, both to set up the function call, including pushing any arguments onto the stack, and to return from the function. For inline functions this is not required.

As for the exectuable being larger, the general rule is:
1) if the inline function is small, the exe will be smaller.
2) if the inline function is large, the exe will be larger.

As for you using inline functions "in all your classes", you can but it's worth remembering "inline" is not a command to your compiler but a request, so it may not end up actually being inline, depending on the function.

The main reasons for a compiler not choosing to make a function inline are often:
1) the function contains a loop, switch or a goto.
2) it's recursive.
3) its contains static variables.

HTH.
 
Enjoyed very clear answer! Thanks!

But why are static variables a problem to the compiler when compiling inline? I'd have thought they were probably the simplest sort of variables that it has to deal with?

I can see that recursion would obviously be impossible in line, because what inline code is really trying to do is insert a set of instructions as they appear into each function which "calls" the inline code, rather than write the code somewhere else and call it. Therefore any function that calls itself would just become an endless loop from the compiler's point of view: a function that it has to keep on starting from scratch again and again...
 
Inline functions take up more memory don't they? Since they paste the function wherever it is being called from. The benefit is the program does less jumping around to get the info. it needs. From what i've heard in large programs this speeds up the program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top