Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

VS.2003, __inline and __forceinline

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pavel A. - 21 Jun 2005 19:35 GMT
Hello,
I'm having strange issues with inline functions in VS.2003.
In following simple code snippet:

extern
int mytmp1( int v ) { return !v; };

extern int mytmp( void )
{
   return mytmp1( -7 );
}

I see mytmp1 expanded inline, despite option /Ob1 (expand only __inline) iin
the project.
Is this normal? Does this depend on C or C++ mode or debug/release mode?

Another question about __inline extensions in pure C mode (MS specific, not
c++):
Are the following statements valid? All they compile clean, but I doubt
whether this can cause "undefined behavour" or is 100% legal:

A. __inline  int  foo1(...) { ... }

B.  int  __inline  foo2( ... ) { ... }

C. __inline int foo3( ...); //  proto
int foo3(...) {... }  // separate body

D. static  __inline int foo3( ...); //  proto
extern int foo3(...) {... }  // __inline with static or extern ??

Could somebody explain please?

Best regards,
Pavel
--
David Lowndes - 23 Jun 2005 11:58 GMT
>I'm having strange issues with inline functions in VS.2003.
>In following simple code snippet:
>...
>I see mytmp1 expanded inline, despite option /Ob1 (expand only __inline) iin
>the project.

Pavel,

Do you have a small console application that illustrates the issue?

Dave
Pavel A. - 25 Jun 2005 16:35 GMT
> >I'm having strange issues with inline functions in VS.2003.
> >In following simple code snippet:
[quoted text clipped - 7 lines]
>
> Dave

Thanks David.
I've tried to get a "minimal" example, and found that the compiler
does very aggressive optimization.
It inlines and removes unused code everywhere.
By itself it is not bad. But why it ignores project options?

This is one variant, where everything gets inlined though
in project settings global optimization is off and inline only explicit.
--------
int /*__inline*/ aaa1( int v )
{ return !v; };

extern int tmp99( void )
{
   printf("test1\n");
   return  aaa1( - 7 ) ;
}

int main(int argc, char* argv[])
{
   tmp99();
   return 0;
}
-------------

My specific question is, why VC syntax in C mode allows
extern __inline and even extern __forceinline ?
Doesn't __inline mean that the function is not visible outside
and can not be extern?

OTOH, if compiler decides to inline a function defined as extern, how
it knows that no another module calls it from outside?

I'm not sure how to post my sample. There is also .vcproj
and other files besides of .c

Regards,
Pavel
Bo Persson - 25 Jun 2005 17:03 GMT
>> >I'm having strange issues with inline functions in VS.2003.
>> >In following simple code snippet:
[quoted text clipped - 33 lines]
>    return 0;
> }

Here is the result I get with your example. Inline /Ob1, global
optimization on

--- c:\documents and settings\...\quick_test.cpp
#include "stdio.h"

int /*__inline*/ aaa1( int v )
{ return !v; };
00401000  xor         eax,eax
00401002  cmp         dword ptr [esp+4],eax
00401006  sete        al
00401009  ret

--- c:\documents and settings\...\quick_test.cpp

extern int tmp99( void )
{
   printf("test1\n");
00401010  push        offset string "test1\n" (406D4Ch)
00401015  call        printf (401038h)
   return  aaa1( - 7 ) ;
0040101A  push        0FFFFFFF9h
0040101C  call        aaa1 (401000h)
00401021  add         esp,8
}
00401024  ret

--- c:\documents and settings\...\quick_test.cpp

int main(int , char* [])
{
   tmp99();
00401030  call        tmp99 (401010h)
   return 0;
00401035  xor         eax,eax
}
00401037  ret

Seems to work very well as intended.

> -------------
>
> My specific question is, why VC syntax in C mode allows
> extern __inline and even extern __forceinline ?
> Doesn't __inline mean that the function is not visible outside
> and can not be extern?

But 'extern' means that it is.  :-)

> OTOH, if compiler decides to inline a function defined as extern, how
> it knows that no another module calls it from outside?

The linker knows. If you delay code generation until link time, the
system knows almost everything about the code.

Bo Persson
David Lowndes - 25 Jun 2005 23:16 GMT
>This is one variant, where everything gets inlined though
>in project settings global optimization is off and inline only explicit.

In your release build project settings, change the optimization
setting to custom rather than the default /O2 or /O1 options. I think
that other optimizations are having an effect.

Dave
Signature

MVP VC++ FAQ: http://www.mvps.org/vcfaq


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.