Problems with the Compiler Optimizer?
Have you ever had problems with the way your compiler optimize your code?
I have very strange things happening to my code and I suspect is the optimizer
(I am programming with HEW for a SuperH architecture)
For example a function like this:
Code:
static char bintochar(const int num)
{
char ch;
int elnum;
elnum=num&0x0F;
//num &= 0x0F;
if (0 <= elnum && elnum <= 9)
ch = '0' + elnum;
else if (10 <= elnum && elnum <= 15)
ch = 'A' + elnum - 10;
return ch;
}
originally it only used num (not elnum). Since it is passed by value it is supposed that num wouldnt change even if its copy were modified inside the function. Well, it changed!
Then I put the const (as above) and used another variable (elnum) so as to assure that num would not change but the optimizer eliminated my variable and again made the parameter (passed by value) changed! Isnt that against all rules in C?!
Another example
Code:
static void afunction(const int media){
int i;
char buf[16];
unsigned long *mc;
unsigned char uc;
unsigned long mcd;
mc = (unsigned long*) 0xa00000F0;
mcd = *mc; //HERE!!!!
buf[0] = 'M';
//....more code
}
I debugged the code and found out that the optimizer made that the local variable mcd and the parameter media share the same memory region! (in fact the same register! therefore when I assign "mcd=*mc" not only mcd changes but media also changes!
I am finding all kinds of these strange things. Anyone has any idea why this is happening?
Thanks a lot in advance
Kansai