Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Inline Functions?

Reply
Thread Tools

Inline Functions?

 
 
raashid bhatt
Guest
Posts: n/a
 
      08-19-2008
hi
by the definition of inline functions
"In computer science, an inline function is a programming language
construct used to suggest to a compiler that a particular function be
subjected to in-line expansion; that is, it suggests that the compiler
insert the complete body of the function in every context where that
function is used."



========================================
int a;

inline void func()
{
a = 50;
}
void __cdecl main()
{

func();
func();

}

===========================================

so the complete body of function func() should actually be placed from
where it is called ... ie this code is similar to

============================================
int a;

void __cdecl main()
{

a = 50;
}
============================================

but when i compile it i get undesired results

PUSH EBP
MOV EBP,ESP
CALL FUNC
; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
SHOUDL BE PLACED HERE
CALL FUNC // SAME HERE
POP EBP
RETN

if i made func as inline then why is it called why not the body of
func is placed at every place it called.

Raashid Bhatt (a.k.a rash0r)
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      08-19-2008
raashid bhatt wrote:

> hi
> by the definition of inline functions
> "In computer science, an inline function is a programming language
> construct used to suggest to a compiler that a particular function be
> subjected to in-line expansion; that is, it suggests that the compiler
> insert the complete body of the function in every context where that
> function is used."
>
>
>
> ========================================
> int a;
>
> inline void func()
> {
> a = 50;
> }
> void __cdecl main()
> {
>
> func();
> func();
>
> }
>
> ===========================================
>
> so the complete body of function func() should actually be placed from
> where it is called ... ie this code is similar to
>
> ============================================
> int a;
>
> void __cdecl main()
> {
>
> a = 50;
> }
> ============================================
>
> but when i compile it i get undesired results
>
> PUSH EBP
> MOV EBP,ESP
> CALL FUNC
> ; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
> SHOUDL BE PLACED HERE
> CALL FUNC // SAME HERE
> POP EBP
> RETN
>
> if i made func as inline then why is it called why not the body of
> func is placed at every place it called.
>
> Raashid Bhatt (a.k.a rash0r)


That's because inline (in C) is just a *suggestion* to the compiler to
make calls to the decorated function as fast as possible. It is not
mandatory that the compiler take your hint. Try turning on
optimisations. Many compilers have specific switches for enabling
inlining and customising the inlining behaviour. Reasearch your
compiler documentation.

PS. Drop the _cdecl attribute from the definition of main and change
it's return type to int. _cdecl is not Standard and in most cases
unnecessary as well.

 
Reply With Quote
 
 
 
 
Luks
Guest
Posts: n/a
 
      08-19-2008
On 19 ago, 02:40, raashid bhatt <(E-Mail Removed)> wrote:
> hi
> by the definition of inline functions
> "In computer science, an inline function is a programming language
> construct used to suggest to a compiler that a particular function be
> subjected to in-line expansion; that is, it suggests that the compiler
> insert the complete body of the function in every context where that
> function is used."
>
> ========================================
> int a;
>
> inline void func()
> {
> * * * * a = 50;}
>
> void __cdecl main()
> {
>
> * * * * func();
> * * * * func();
>
> }
>
> ===========================================
>
> so the complete body of function func() should actually be placed from
> where it is called ... ie this code is similar to
>
> ============================================
> int a;
>
> void __cdecl main()
> {
>
> * * * * a = 50;}
>
> ============================================
>
> but when i compile it i get undesired results
>
> PUSH EBP
> MOV EBP,ESP
> CALL FUNC
> ; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
> SHOUDL BE PLACED HERE
> CALL FUNC * *// SAME HERE
> POP EBP
> RETN
>
> if i made func as inline then why is it called why not the body of
> func is placed at every place it called.
>
> Raashid Bhatt (a.k.a rash0r)


Maybe inline functions only suggests to the compiler that desired
behavior. It's a matter of compiler implementation, i believe
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-19-2008
raashid bhatt <(E-Mail Removed)> writes:
> by the definition of inline functions
> "In computer science, an inline function is a programming language
> construct used to suggest to a compiler that a particular function be
> subjected to in-line expansion; that is, it suggests that the compiler
> insert the complete body of the function in every context where that
> function is used."
>
>
>
> ========================================
> int a;
>
> inline void func()
> {
> a = 50;
> }
> void __cdecl main()
> {
>
> func();
> func();
>
> }
>
> ===========================================
>
> so the complete body of function func() should actually be placed from
> where it is called ... ie this code is similar to
>
> ============================================
> int a;
>
> void __cdecl main()
> {
>
> a = 50;
> }
> ============================================
>
> but when i compile it i get undesired results
>
> PUSH EBP
> MOV EBP,ESP
> CALL FUNC
> ; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
> SHOUDL BE PLACED HERE
> CALL FUNC // SAME HERE
> POP EBP
> RETN
>
> if i made func as inline then why is it called why not the body of
> func is placed at every place it called.


A C implementation isn't required to generate the machine code you
expect it to generate. In fact, it isn't (as far as the C standard is
concerned) required to generate code at all. Quoting the standard
(C99 5.1.2.3p5):

The least requirements on a conforming implementation are:

-- At sequence points, volatile objects are stable in the sense
that previous accesses are complete and subsequent accesses
have not yet occurred.

-- At program termination, all data written into files shall be
identical to the result that execution of the program according
to the abstract semantics would have produced.

-- The input and output dynamics of interactive devices shall take
place as specified in 7.19.3. The intent of these requirements
is that unbuffered or line-buffered output appear as soon as
possible, to ensure that prompting messages actually appear
prior to a program waiting for input.

In short, the *visible behavior* of a program must be as defined by
the standard. Since your program has no visible behavior, it's
equivalent to

int main(void) { return 0; }

and a compiler could, and quite possibly would, generate equivalent
code for it.

This ignores the error of declaring main as "void __cdecl main()"; it
should be "int main(void)".

As for whether the code for the function is actually inlined at the
point of the call, the standard specifically doesn't require that; the
"inline" keyword merely "suggests that calls to the function be as
fast as possible". One typical way to satisfy that suggestion is, as
the word suggests, to generate inline code rather than a function
call. (It's likely that your compiler will do this only if you use
some option to request optimization.) But ignoring the suggestion is
acceptable.

Examining the assembly code generated for a C program can be useful,
but its usefulness is strictly limited. The test of whether a C
compiler works properly is whether the program behaves correctly.
Generating assembly code (or machine code) is merely a means to that
end. C is not a language for specifying machine code; it's a language
for specifying program behavior.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
To inline or not to inline? Alvin C++ 7 05-06-2005 03:04 PM
Function delcared inline but not defined inline Nish C Programming 4 10-08-2004 03:31 PM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments