CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Bug: Int16 overwrites data in call by reference

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

Bug: Int16 overwrites data in call by reference
PostPosted: Fri Apr 23, 2004 5:49 am     Reply with quote

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
PostPosted: Fri Apr 23, 2004 5:57 am     Reply with quote

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
PostPosted: Sun Apr 25, 2004 8:21 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 26, 2004 7:42 am     Reply with quote

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).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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