Array arithmetic conversion addition bug

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Darryl
Posts: 23
Joined: 28 Jun 2007 21:20

Array arithmetic conversion addition bug

#1 Post by Darryl » 01 Apr 2009 08:46

It appears the following statements add incorrectly:

unsigned long aulHold[2];
unsigned int uiT;

aulHold[ucChan] = aulHold[ucChan] + uiT;
or
aulHold[ucChan] += uiT;

This was tested with both operands being zero, but it may have problems with other values.

It appears to be a conversion problem. Casting uiT to an unsigned long will appear to solve the problem for operands that are zero, but will not add correctly. Changing uiT to an unsigned long appears to fix the problem.

This problem does not appear to occur (in at least some cases) when the unsigned long variable is not an array (i.e. a simple variable). I have not tested when the variable is accessed through a pointer or in a structure.

If this is code dependant, please let me know and I will send sample code.
Last edited by Darryl on 01 Apr 2009 23:12, edited 1 time in total.

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

Re: Array arithmetic conversion addition bug

#2 Post by rajkovic » 01 Apr 2009 08:55

Darryl wrote:It appears the following statements add incorrectly:

unsigned long aulHold[2];
unsigned int uiT;

aulHold[ucChan] = aulHold[ucChan] + uiT;
or
aulHold[ucChan] += uiT;

This was tested with both operands being zero, but it may have problems with other values.

It appears to be a conversion problem as either changing uiT to an unsigned long or casting it to an unsigned long fix the problem.

This problem does not appear to occur (in at least some cases) when the unsigned long variable is not an array (i.e. a simple variable). I have not tested when the variable is accessed through a pointer or in a structure.

If this is code dependant, please let me know and I will send sample code.
Please post or send sample code to rajkovic@mikroe.com
together with description what is the problem.
We had not been able to reproduce problem (is it p16 or p18)

Post Reply

Return to “mikroC PRO for PIC General”