Re: Inline Functions?



raashid bhatt <raashidbhatt@xxxxxxxxx> 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) kst-u@xxxxxxx <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"
.



Relevant Pages

  • Re: a modest proposal
    ... end inline ... until exit back to fortran syntax on encountering the "end inline" ... inline statement that allowed the insertion of assy language statements, ... The XPL compiler was written in itself. ...
    (comp.lang.fortran)
  • Re: Inline Functions?
    ... an inline function is a programming language ... that is, it suggests that the compiler ... void __cdecl main ... so the complete body of function func() should actually be placed from ...
    (comp.lang.c)
  • Re: wits end
    ... I understand the need for syntax, ... the compiler follows the rules of the language, ... syntax for a function call with no arguments is func; ...
    (comp.lang.c)
  • Re: OO compilers and efficiency
    ... In a statically compiled language, if the compiler can prove that the ... Even if it is technically possible to inline like this it often doesn't ... In C++ for example the compiler often compiles file by file. ... The existing register ...
    (comp.programming)
  • Re: a modest proposal
    ... end inline ... revert the compiler to its NATIVE language on encountering the "inline" ... The compiler would have to have an extended syntax in order to ...
    (comp.lang.fortran)