Inline Functions?

R

raashid bhatt

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)
 
S

santosh

raashid said:
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.
 
L

Luks

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
 
K

Keith Thompson

raashid bhatt said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

inline 5
inline functions 50
Inline functions and warning 7
Inline not working properly in GCC? 3
inline functions 3
Inline functions 33
Ambiguity while overloading functions 9
inline functions 5

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top