|
|
View previous topic :: View next topic |
Author |
Message |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
Bug: Int16 overwrites data in call by reference |
Posted: Fri Apr 23, 2004 5:49 am |
|
|
Just for your info: I reported a compiler bug in PCH v3.187. When incrementing a 16 bit integer and assigning the result to a referenced int8 variable, other data is overwritten.
Code: |
#include <18F458.h>
#fuses HS,NOWDT,OSCSEN,PUT,BROWNOUT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=38400, parity=N, xmit=PIN_D1, rcv=PIN_D0)
#separate void Test(int8 &A, int8 &B)
{
int16 C;
B = 1;
C = 2;
A = C + 1; // High nibble of result overwrites B !!!
}
void main()
{
int8 A, B;
Test(A, B);
printf("A= %d\n\r", A); // A= 3
printf("B= %d\n\r", B); // B= 0 ERROR, must be 1 !!!
}
|
A workaround is:
Code: |
Change
A = C + 1;
to:
A = (int8)C + 1;
|
It's a bug and at least the compiler should have issued a warning. |
|
|
Ttelmah Guest
|
Re: Bug: Int16 overwrites data in call by reference |
Posted: Fri Apr 23, 2004 5:57 am |
|
|
ckielstra wrote: | Just for your info: I reported a compiler bug in PCH v3.187. When incrementing a 16 bit integer and assigning the result to a referenced int8 variable, other data is overwritten.
Code: |
#include <18F458.h>
#fuses HS,NOWDT,OSCSEN,PUT,BROWNOUT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=38400, parity=N, xmit=PIN_D1, rcv=PIN_D0)
#separate void Test(int8 &A, int8 &B)
{
int16 C;
B = 1;
C = 2;
A = C + 1; // High nibble of result overwrites B !!!
}
void main()
{
int8 A, B;
Test(A, B);
printf("A= %d\n\r", A); // A= 3
printf("B= %d\n\r", B); // B= 0 ERROR, must be 1 !!!
}
|
A workaround is:
Code: |
Change
A = C + 1;
to:
A = (int8)C + 1;
|
It's a bug and at least the compiler should have issued a warning. |
I'm not suprised.
CCS, is _very_ loose about types of pointers. It can be useful (allowing you to pass an integer pointer, and then using this to address other data). As you say, the compiler should really just 'warn' that the operation is producing a type larger than the data type defined in the function call. Many people have in the past used other C 'syntax checkers', to scan code before compiling with CCS, to trap exactly this type of problem.
Best Wishes |
|
|
guest Guest
|
type casting |
Posted: Sun Apr 25, 2004 8:21 pm |
|
|
Another workaround is:
Code:
Change
A = C + 1;
to:
C = C + 1
A = C;
However, explicit type casting is prefered for the _loose_ CCS compiler. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Apr 26, 2004 7:42 am |
|
|
I'm positively surprised by the quick response from CCS:
"The problem you reported has been fixed and will be in the
next compiler release."
(current version = 3.190). |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|