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
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
manjut19
...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?
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
VictorN
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
Re: Compiler optimization problem in VS 2008
Why is iExisist defined as volatile? It would be useful if you posted the code for SearchEntry().
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
manjut19
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.
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
manjut19
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
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.
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
manjut19
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
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
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
2kaud
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.
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
manjut19
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?
Re: Compiler optimization problem in VS 2008
Quote:
Originally Posted by
manjut19
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