Re: Inline Functions?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 19 Aug 2008 00:58:20 -0700
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"
.
- References:
- Inline Functions?
- From: raashid bhatt
- Inline Functions?
- Prev by Date: Re: K&R Ex 1-22
- Next by Date: Re: K&R p 130
- Previous by thread: Re: Inline Functions?
- Next by thread: K&R Ex 1-22
- Index(es):
Relevant Pages
|