Bug in VC++ 6.0?
On gcc, the following code gives (and should give, in my opinion) -128 -128. But in VC++, it gives something else:
-128 65408. Is this a bug in Visual C++?
#include
signed short sin_array[2];
int in_array[2];
int main(int argc, char** argv)
{
int p = 0;
/* this signed short cast seems to cast to unsigned short */
sin_array[p++] = (signed short)in_array[p] = -128;
printf("%d %d",sin_array[0],in_array[0]);
return 0;
}
Really. I can''t even get it to compile on VC 6. It gives a C2106 on the assignment.
Well this code would not compile for me. I got the error "error C2106: ''='' : left operand must be l-value." After i took that cast though it compiled and the output was -128 -128. Im on Visual C++ 6.0 Enterprise, i dont know which service pack, probably 5.
-SirKnight
-SirKnight
It'll work if you do this:
sin_array[p++] = (signed short)(in_array[p] = -128);
Which makes sense ... IIRC, casts have a higher precedence than assigment operators.
~~~~~~~~~~
Martee
Edited by - Martee on July 10, 2001 10:11:20 PM
sin_array[p++] = (signed short)(in_array[p] = -128);
Which makes sense ... IIRC, casts have a higher precedence than assigment operators.
~~~~~~~~~~
Martee
Edited by - Martee on July 10, 2001 10:11:20 PM
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Thank you all, especially Martee, who demystified the problem. I was using VC++ version 6 SP3. I guess the moral here is use parentheses when operator precedence is not obvious.
This article:
http://support.microsoft.com/support/kb/articles/Q243/4/51.ASP
..lists all of the noncompliance issues in VC++ 6.0. If you ever have a problem that is not one of these issues, the fault is with you or your library, not the compiler. You should view this page before posting something titled "bug in C++".
http://support.microsoft.com/support/kb/articles/Q243/4/51.ASP
..lists all of the noncompliance issues in VC++ 6.0. If you ever have a problem that is not one of these issues, the fault is with you or your library, not the compiler. You should view this page before posting something titled "bug in C++".
July 11, 2001 02:05 PM
sin_array[p++] = (signed short)in_array[p] = -128;
This is undefined. Therefore the results are undefined and unpredictable.
This is undefined. Therefore the results are undefined and unpredictable.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement