CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2013
    Posts
    77

    Compiler optimization problem in VS 2008

    Hi all,
    I am developing a win 32 application on Visual studio2008.In realease mode with compiler optimization disabled ,my application working fine.But,in maximize speed(Compiler optimization field in C++)its not working properly,Iam explaining with a code
    Code:
    volatile int iExisist=-1;
    iExisist=SearchEntry(_T("abc"));//Searching an entry  
    if(iExiist!=-1)
    {
    	MessageBox(hWnd, _T("Entry Found"), _T("Success"), MB_OK );
    }
    else
    	MessageBox(hWnd, _T("Entry not Found"), _T("ERROR"), MB_OK |MB_ICONERROR);
    While running the application with Compiler optimization disabled Working Fine.In enabled condition(Compiler optimization(Maximize Speed)))always going to else part eventhough i am giving a correct entry.What may be the reason for this problem.Please help me

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by manjut19 View Post
    ...In enabled condition(Compiler optimization(Maximize Speed)))always going to else part eventhough i am giving a correct entry.What may be the reason for this problem.Please help me
    Define "correct entry". How did you know it was "correct"?
    What does the SearchEntry function do?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2013
    Posts
    77

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by VictorN View Post
    Define "correct entry". How did you know it was "correct"?
    What does the SearchEntry function do?

    SearchEntry Function will saech the passing string on a Array of string suppose
    Code:
    TCHAR *p[]={_T("abc"),_T("xyz"))
    SearchEntry function search string abc into this array p.
    "correct entry"means the passing string is there in "p"
    if abc string will find on "p"
    and then function will return a value "0"else"-1".This fuction i debugged in "Optimization disabled "state.I am getting the values correctly and code working fine.Running time also no problem,But In "with optimization" not working properly
    Last edited by manjut19; November 21st, 2013 at 03:30 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Compiler optimization problem in VS 2008

    Why is iExisist defined as volatile? It would be useful if you posted the code for SearchEntry().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by manjut19 View Post
    While running the application with Compiler optimization disabled Working Fine.In enabled condition(Compiler optimization(Maximize Speed)))always going to else part eventhough i am giving a correct entry.What may be the reason for this problem.Please help me
    You have a bug in your program. Without seeing all the relevant source code, we cannot help you.
    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

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by manjut19 View Post
    Code:
    volatile int iExisist=-1;
    iExisist=SearchEntry(_T("abc"));//Searching an entry  
    if(iExiist!=-1)
    ...
    Here is your problem: you are setting variable iExisist, while checking iExiist
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Compiler optimization problem in VS 2008

    I suspect this is a typing mistake when the code was entered into the post as the OP says that the code 'works' when not optimised as a misspelt variable would generate a compiler error.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by manjut19 View Post
    SearchEntry Function will saech the passing string on a Array of string suppose
    Code:
    TCHAR *p[]={_T("abc"),_T("xyz"))
    SearchEntry function search string abc into this array p.
    "correct entry"means the passing string is there in "p"
    if abc string will find on "p"
    and then function will return a value "0"else"-1".This fuction i debugged in "Optimization disabled "state.I am getting the values correctly and code working fine.Running time also no problem,But In "with optimization" not working properly
    So you're saying that one of the most used compilers in the world, used by thousands of programmers every day, cannot produce a proper working program from such simple code? You know that can't be the issue -- the problem is definitely your programming.

    First, you need to show your SearchEntry function. I will make a bet -- I bet that you are not searching the array correctly.

    The reason why I think this is that you have an array of string literals. Are you comparing your string literals using == to determine if the string is in the array? If you are, then your function is wrong. You don't compare C-style strings using ==.
    Code:
    int SearchEntry(TCHAR *pStr)
    {
         TCHAR *p[]={_T("abc"),_T("xyz"));
         for (int i = 0; i < 2; ++i )
         {
             if ( pStr == p[i] ) // Wrong!!!
                 return i;
         }
         return -1;
    }
    If your function looks anything like that (using a while loop, do-while, whatever), and you're using == to compare, then that is wrong.

    Again, show your SearchEntry function.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 21st, 2013 at 05:58 PM.

  9. #9
    Join Date
    Apr 2013
    Posts
    77

    Re: Compiler optimization problem in VS 2008

    My problem got solved.that is because of,I was using a string copying function in Search function.I was using wcscpy()instead of wcscpy_s().Thanks for all for your valuable reply

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by 2kaud View Post
    I suspect this is a typing mistake when the code was entered into the post as the OP says that the code 'works' ...
    I figured that
    But, since we didn't have the real code, I made an observation on what we did see.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  11. #11
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by manjut19 View Post
    My problem got solved.that is because of,I was using a string copying function in Search function.I was using wcscpy()instead of wcscpy_s().
    Curiouser and curiouser! Why do you need to make a copy in the search function? Could you please post the *real* code?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Compiler optimization problem in VS 2008

    Quote Originally Posted by manjut19 View Post
    My problem got solved.that is because of,I was using a string copying function in Search function.I was using wcscpy()instead of wcscpy_s().Thanks for all for your valuable reply
    But as Vladimir stated, why do you need to do any copying?

    You stated that you had a static array, and you want to see if a string is an entry in the array. So explain why you need to do any copying.

    From what you described, all you need to do is this:
    Code:
    int SearchEntry(TCHAR *pStr)
    {
         TCHAR *p[]={_T("abc"),_T("xyz"));
         for (int i = 0; i < 2; ++i )
         {
             if ( _tcscmp(pStr,p[i]) == 0 ) 
                 return i;
         }
         return -1;
    }
    Also, why are you using wcscpy_s() for TCHAR strings? You're supposed to be using the TCHAR routines for TCHAR strings, not specific wide or MBCS functions.

    Regards,

    Paul McKenzie

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