LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   g++: strange compiler warning (https://www.linuxquestions.org/questions/linux-software-2/g-strange-compiler-warning-801730/)

thesource2 04-13-2010 07:32 AM

g++: strange compiler warning
 
g++ compiler (4.4.3-4) gives me a strange warning on the following code:
T & GetElem(int64 i)
{
if((i<0ll)||(i>=m_iSize))
return m_cDummy;
return m_pData[i];
}
Where T - template argument (it's a simple template dynamic array). m_iSize is int64 (long long). m_cDummy is T, m_pData is T*

The warning is: warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false

Does any one know why I get this?

ilikejam 04-13-2010 10:55 AM

Looks like gcc has optimised out your if statement (assuming that's where the warning came from), and is warning you that the optimised code may not be correct if the values overflow.

See:
http://patrakov.blogspot.com/2008_10_01_archive.html

Dave

thesource2 04-14-2010 04:49 AM

Thanks for reply. The only question is why gcc optimizes my 'if' if I use -O0 option.

ilikejam 04-14-2010 05:17 AM

Hmmm. I'm not particularly familiar with the details of how gcc operates internally - maybe it's not an optimisation, but just how the if comparisons are converted to machine code?

Dave


All times are GMT -5. The time now is 10:25 PM.