Thread: When does the compiler listen to the inline specifier

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    156

    When does the compiler listen to the inline specifier

    This simple program does not expand the inline functions. After the code there is a listing of masm and source code showing its not inlining. Rather it calling the function doin' the whole epilog then function then prolog rather than expanding the code in place. I've tried without the inline specifier which defaults to inline when defined in the class (which it is).

    Code:
    #include "stdafx.h"
    
    class tstring
    {
    
    	long m_nTStringLength;
    
    public:
    
    	inline tstring( int nLength = 100 ) : m_nTStringLength( nLength )
    	{	}
    
    	inline int GetTString( )
    	{
    		return m_nTStringLength;
    	}
    };
    
    int main(int argc, char* argv[])
    {
    
    	tstring t1;
    
    	long i = t1.GetTString( );
    
    	return 0;
    
    }
    Generates:

    Code:
    	TITLE	C:\Practice\tstringTest\tstringTest.cpp
    PUBLIC	??0tstring@@QAE@H@Z				; tstring::tstring
    PUBLIC	?GetTString@tstring@@QAEHXZ			; tstring::GetTString
    PUBLIC	_main
    EXTRN	__chkesp:NEAR
    ;	COMDAT _main
    _TEXT	SEGMENT
    _t1$ = -4
    _i$ = -8
    _main	PROC NEAR					; COMDAT
    
    ; 26   : {
    
    	push	ebp
    	mov	ebp, esp
    	sub	esp, 72					; 00000048H
    	push	ebx
    	push	esi
    	push	edi
    	lea	edi, DWORD PTR [ebp-72]
    	mov	ecx, 18					; 00000012H
    	mov	eax, -858993460				; ccccccccH
    	rep stosd
    
    ; 27   : 
    ; 28   : 	tstring t1;
    
    	push	100					; 00000064H
    	lea	ecx, DWORD PTR _t1$[ebp]
    	call	??0tstring@@QAE@H@Z			; tstring::tstring
    
    ; 29   : 
    ; 30   : 	long i = t1.GetTString( );
    
    	lea	ecx, DWORD PTR _t1$[ebp]
    	call	?GetTString@tstring@@QAEHXZ		; tstring::GetTString
    	mov	DWORD PTR _i$[ebp], eax
    
    ; 31   : 
    ; 32   : 	return 0;
    
    	xor	eax, eax
    
    ; 33   : 
    ; 34   : }
    
    	pop	edi
    	pop	esi
    	pop	ebx
    	add	esp, 72					; 00000048H
    	cmp	ebp, esp
    	call	__chkesp
    	mov	esp, ebp
    	pop	ebp
    	ret	0
    _main	ENDP
    _TEXT	ENDS
    ;	COMDAT ??0tstring@@QAE@H@Z
    _TEXT	SEGMENT
    _nLength$ = 8
    _this$ = -4
    ??0tstring@@QAE@H@Z PROC NEAR				; tstring::tstring, COMDAT
    
    ; 17   : 	{	}
    
    	push	ebp
    	mov	ebp, esp
    	sub	esp, 68					; 00000044H
    	push	ebx
    	push	esi
    	push	edi
    	push	ecx
    	lea	edi, DWORD PTR [ebp-68]
    	mov	ecx, 17					; 00000011H
    	mov	eax, -858993460				; ccccccccH
    	rep stosd
    	pop	ecx
    	mov	DWORD PTR _this$[ebp], ecx
    	mov	eax, DWORD PTR _this$[ebp]
    	mov	ecx, DWORD PTR _nLength$[ebp]
    	mov	DWORD PTR [eax], ecx
    	mov	eax, DWORD PTR _this$[ebp]
    	pop	edi
    	pop	esi
    	pop	ebx
    	mov	esp, ebp
    	pop	ebp
    	ret	4
    ??0tstring@@QAE@H@Z ENDP				; tstring::tstring
    _TEXT	ENDS
    ;	COMDAT ?GetTString@tstring@@QAEHXZ
    _TEXT	SEGMENT
    _this$ = -4
    ?GetTString@tstring@@QAEHXZ PROC NEAR			; tstring::GetTString, COMDAT
    
    ; 20   : 	{
    
    	push	ebp
    	mov	ebp, esp
    	sub	esp, 68					; 00000044H
    	push	ebx
    	push	esi
    	push	edi
    	push	ecx
    	lea	edi, DWORD PTR [ebp-68]
    	mov	ecx, 17					; 00000011H
    	mov	eax, -858993460				; ccccccccH
    	rep stosd
    	pop	ecx
    	mov	DWORD PTR _this$[ebp], ecx
    
    ; 21   : 		return m_nTStringLength;
    
    	mov	eax, DWORD PTR _this$[ebp]
    	mov	eax, DWORD PTR [eax]
    
    ; 22   : 	}
    
    	pop	edi
    	pop	esi
    	pop	ebx
    	mov	esp, ebp
    	pop	ebp
    	ret	0
    ?GetTString@tstring@@QAEHXZ ENDP			; tstring::GetTString
    _TEXT	ENDS
    END
    thanks

    dang

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It should be inlining those functions. The only thing I can think of that you might try is to make 'm_nTStringLength' an int so that no type-casting goes on in those functions.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156

    When does the compiler listen to the inline specifier

    Thanks, but still won't inline. I've even fooled around with the /obx compiler optimization command for inline expansion and no luck.

    thanks


    dang

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if you are using visual c (and i guess you are) then check out __forceinline.

    inline, __inline, __forceinline
    inline

    function_declarator; // C++ Specific

    __inline

    function_declarator;// Microsoft Specific

    __forceinline

    function_declarator;// Microsoft Specific

    The inline and __inline keywords allow the compiler to insert a copy of the function body into each place the function is called. The insertion occurs only if the compiler's cost/benefit analysis show it to be profitable. The __forceinline keyword overrides the cost/benefit analysis and relies on the judgement of the programmer instead. Exercise caution when using __forceinline. Indiscriminate use of __forceinline can result in larger code with only marginal performance gains or, in some cases, even performance losses (due to increased paging of a larger executable, for example).

    You cannot force the compiler to inline a function when conditions other than cost/benefit analysis prevent it. You cannot inline a function if:

    The function or its caller is compiled with /Ob0 (the default option for debug builds).


    The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other).


    The function has a variable argument list.


    The function uses inline assembly and is not compiled with /Og, /Ox, /O1, or /O2).


    Function returns an unwindable object by value and is not compiled with /GX, /EHs, or /EHa).


    The function receives a copy-constructed object passed by value, when compiled with /GX, /EHs,, or /EHa.


    The function is recursive and is not accompanied by #pragma(inline_recursion, on). With the pragma, recursive functions can be inlined to a default depth of eight calls. To change the inlining depth, use #pragma(inline_depth, n).
    If the compiler cannot inline a function declared __forceinline, it generates a level 1 warning (4714).

    The inline keyword is available only in C++. The __inline and __forceinline keywords are available in both C and C++. For compatibility with previous versions, _inline is a synonym for __inline.

    Using inline functions can make your program faster because they eliminate the overhead associated with function calls. Functions expanded inline are subject to code optimizations not available to normal functions.

    The /Ob compiler optimization option determines whether inline function expansion actually occurs.

    For related information, see auto_inline.

    Example 1

    inline int max( int a , int b )
    {
    if( a > b ) return a;
    return b;
    }
    A class's member functions can be declared inline either by using the inline keyword or by placing the function definition within the class definition.

    Example 2

    class MyClass
    {
    public:
    void print() { cout << i << ''; } // Implicitly inline
    private:
    int i;
    };
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    156

    When does the compiler listen to the inline specifier

    Thank Stoned_Coder that did the trick.

    Thanks alot!

    dang

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Are you compiling the debug build? The compiler can't inline functions if the /ZI compiler option is enabled. I compiled your code in release build and by changing the debug option to /Zi (and enabled inling in optimisations) and in both cases even without the inline specifiers in the source code the functions were inlined in the asm output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev C++ Compiler, Indentation?
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:13 AM
  2. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  3. bit shifting
    By Nor in forum C++ Programming
    Replies: 9
    Last Post: 08-08-2003, 11:55 AM
  4. inline??
    By bman1176 in forum C++ Programming
    Replies: 2
    Last Post: 06-25-2002, 05:31 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM