CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Jan 2011
    Posts
    101

    [c++]code executon

    Hello everybody, my first post on this forum.

    I am starting to learning c++ language, i got myself a msv c++ 2010 express edition, downloaded/bookmarked lots of tutorials here on the internet so i have some question/s that might sound stupid to you that i want to ask, anyway, here it goes:
    consider following code (pseudo)
    Code:
    if(expression1)
    {
        if(expression2)
        {
            //some complicated task
        }
    }
    Does second if statement got evaluated if first is false?

    I start debugging and looked into disassembly (Debug/Windows/Disassembly), compare two chars, first if statement:
    [code]
    01071548 movsx eax,byte ptr [a]
    0107154C movsx ecx,byte ptr [b]
    01071550 cmp eax,ecx
    01071552 jne main+8Bh (107158Bh) <-- about this
    [code]
    I am not familiar with assembly, i want to ask about last instruction what does it do?

  2. #2
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: [c++]code executon

    no, the second if statement is not evaluated

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: [c++]code executon

    It depends on how the compiler handles it, taking optimizations into account.

  4. #4
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: [c++]code executon

    ...the first if statement is what executes the second

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: [c++]code executon

    Quote Originally Posted by iiSoMeGuY 7x View Post
    no, the second if statement is not evaluated
    It could be.

    e.g.

    Code:
    if(var1 == 42)
    {
        if(var2 == 42)
        {
            do_something()
        }
    }
    could be better assembled as:

    Code:
    //pseudo 
    mov reg, var1
    and reg, var2
    cmp reg, 42
    je _do_something

  6. #6
    Join Date
    Jan 2011
    Posts
    101

    Re: [c++]code executon

    Let say that in second if stement, right operand (literal constant in your case) is given by
    executing complicated function, something like:
    Code:
    if( blah )
    {
        if( var2 == HeavyFunction() )
        {
            //some complicated task
        }
    }

  7. #7
    Join Date
    Aug 2008
    Posts
    902

    Re: [c++]code executon

    Quote Originally Posted by borko1980 View Post
    Let say that in second if stement, right operand (literal constant in your case) is given by
    executing complicated function, something like:
    Code:
    if( blah )
    {
        if( var2 == HeavyFunction() )
        {
            //some complicated task
        }
    }
    Probably not. I just wanted to point out that it was conceivably possible for the compiler to optimize things away. The best way to tell is to actually look at the compiled code.

  8. #8
    Join Date
    Jan 2011
    Posts
    101

    Re: [c++]code executon

    ...look at the compiled code.
    In case of my compiler, where?

  9. #9
    Join Date
    Aug 2008
    Posts
    902

    Re: [c++]code executon

    Quote Originally Posted by borko1980 View Post
    In case of my compiler, where?
    In a debugger or disassembler.

  10. #10
    Join Date
    Jan 2011
    Posts
    101

    Re: [c++]code executon

    The whole point of my question is that i want to avoid evaluation of second if statement if first is false, because second is very expensive.By some human/normal logic "iiSoMeGuY 7x" should be right but as you said it depends on the compiler, so now i don't know if this is good coding practice to continue with?
    How/where (if possible) to see assembly of compiled code?

  11. #11
    Join Date
    Aug 2008
    Posts
    902

    Re: [c++]code executon

    Well, you should be able to trust your compiler to do the right thing. It wouldn't be an optimization if the compiler chose to evaluate a lengthy boolean that wasn't necessary.

  12. #12
    Join Date
    Jan 2011
    Posts
    101

    Re: [c++]code executon

    OK, here is quick sample from msv:
    C++
    Code:
    #include <iostream>
    
    int main()
    {
    	const char a = 'a';
    	char b, c;
    	std::cin >> b;
    	std::cin >> c;
    
    	if(a == b)
    	{
    		std::cout << "a = b" << std::endl;
    		if(b == c)
    		{
    			std::cout << "b = c" << std::endl;
    		}
    	}
    
    	std::cin.get();
    	return 1;
    }
    disassembly, i putted breakpoint into std::cin.get() line and input "z" as char so entire if statements should be avoided
    Code:
    const char a = 'a';
    00F6151E  mov         byte ptr [a],61h  
    	char b, c;
    	std::cin >> b;
    00F61522  lea         eax,[b]  
    00F61525  push        eax  
    00F61526  mov         ecx,dword ptr [__imp_std::cin (0F6A32Ch)]  
    00F6152C  push        ecx  
    00F6152D  call        std::operator>><char,std::char_traits<char> > (0F61055h)  
    00F61532  add         esp,8  
    	std::cin >> c;
    00F61535  lea         eax,[c]  
    00F61538  push        eax  
    00F61539  mov         ecx,dword ptr [__imp_std::cin (0F6A32Ch)]  
    00F6153F  push        ecx  
    00F61540  call        std::operator>><char,std::char_traits<char> > (0F61055h)  
    00F61545  add         esp,8  
    
    	if(a == b)
    00F61548  movsx       eax,byte ptr [b]  
    00F6154C  cmp         eax,61h  
    00F6154F  jne         main+0B3h (0F615B3h)  
    	{
    		std::cout << "a = b" << std::endl;
    00F61551  mov         esi,esp  
    00F61553  mov         eax,dword ptr [__imp_std::endl (0F6A330h)]  
    00F61558  push        eax  
    00F61559  push        offset string "a = b" (0F67898h)  
    00F6155E  mov         ecx,dword ptr [__imp_std::cout (0F6A334h)]  
    00F61564  push        ecx  
    00F61565  call        std::operator<<<std::char_traits<char> > (0F61168h)  
    00F6156A  add         esp,8  
    00F6156D  mov         ecx,eax  
    00F6156F  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0F6A338h)]  
    00F61575  cmp         esi,esp  
    00F61577  call        @ILT+435(__RTC_CheckEsp) (0F611B8h)  
    		if(b == c)
    00F6157C  movsx       eax,byte ptr [b]  
    00F61580  movsx       ecx,byte ptr [c]  
    00F61584  cmp         eax,ecx  
    00F61586  jne         main+0B3h (0F615B3h)  
    		{
    			std::cout << "b = c" << std::endl;
    00F61588  mov         esi,esp  
    00F6158A  mov         eax,dword ptr [__imp_std::endl (0F6A330h)]  
    00F6158F  push        eax  
    00F61590  push        offset string "b = c" (0F67830h)  
    00F61595  mov         ecx,dword ptr [__imp_std::cout (0F6A334h)]  
    00F6159B  push        ecx  
    00F6159C  call        std::operator<<<std::char_traits<char> > (0F61168h)  
    00F615A1  add         esp,8  
    00F615A4  mov         ecx,eax  
    00F615A6  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0F6A338h)]  
    00F615AC  cmp         esi,esp  
    00F615AE  call        @ILT+435(__RTC_CheckEsp) (0F611B8h)  
    		}
    	}
    As i read this it only shows what each expression in c++ does in assembly but not what is actually executed, does above means that its executing second if statement?

  13. #13
    Join Date
    Aug 2008
    Posts
    902

    Re: [c++]code executon

    No it doesn't. The compiler can't know what b or c will be at the time it compiles, so it behaves exactly as you would expect using the same logic as iiSoMeGuY 7x.

  14. #14
    Join Date
    Jan 2011
    Posts
    101

    Re: [c++]code executon

    OK. Thanks greatly on your help.

  15. #15
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: [c++]code executon

    Quote Originally Posted by borko1980 View Post
    Does second if statement got evaluated if first is false?
    No, and it also doesn't get executed when you do
    Code:
    if (statement1 && statement2) {
        // do something
    }
    If statement1 is false, statement2 will not be executed. Similarly for || (logical OR).
    Quote Originally Posted by borko1980 View Post
    I am not familiar with assembly, i want to ask about last instruction what does it do?
    There's no need to learn assembly in order to learn C++. The assembly outputted by your compiler can differ according to the compiler, compiler version and compiler settings used to compile the code. What shouldn't differ is the observable behavior of your program, to the extent that your program is written in standard C++.

    What I explained above is guaranteed by standard C++. So we can even give statement2 side-effects and rely on the fact that it will not be executed when statement1 is false.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured