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 support@ccsinfo.com

[PIC24HJ256GP210] Increment operator issue with int32 array

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



Joined: 13 Feb 2019
Posts: 24

View user's profile Send private message

[PIC24HJ256GP210] Increment operator issue with int32 array
PostPosted: Wed Jun 19, 2019 7:11 am     Reply with quote

Before I get started, this post is mostly to have a record on the internet for others with similar issues since I couldn't find anything else like it.

PIC-C Version 4.106 (very old)

Problem

I have an array of 32 bit signed integers:

Code:
signed int32 Count[6];


I have a loop that gets called over 100k times, and increments a particular element in the array like so:

Code:
Count[ID]++;


The number of times the loop is called is variable (based on user input/configuration).

When the loop was called more than a certain amount of times, the count would be wrong. It was very easy to see what happened:

0x0001FFFF -> 0x00010000

It failed to increment the most significant 2 bytes.

Workaround

There is an easy way to deal with this:

Code:
Count[ID] = Count[ID] + 1;


For some reason, this properly increments the Count element, unlike the ++ operator.

Why?

Has anyone seen this before, and can they tell me what's going on?

* Issue with compiler?
* Silicon Errata?
* My understanding of C for the last 10 years is fundamentally flawed?

Thanks!
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed Jun 19, 2019 7:45 am     Reply with quote

I have no idea about the questions you posted at the end, but I am curious: What if you use ++Count[ID] instead of Count[ID]++?
What about Count[ID] += 1?

As I understand it post incrementing, it needs to save the current value before incrementing the value, and then it returns the current value. In contrast, preincrementing just increments the value and returns the incremented value. There's less steps there...

So there might be something to do with that?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Jun 19, 2019 8:55 am     Reply with quote

It is specific to the low 4.1xx compilers.
Just wrote a basic test. Works find if the array index is fixed.
Fails on int32 or unsigned int32, at the wrap from 0x1FFFF to 0x20000.
In 4.141, it happily works OK.
sum[ID]+=1l
works OK.
canadidan



Joined: 13 Feb 2019
Posts: 24

View user's profile Send private message

PostPosted: Wed Jun 19, 2019 9:18 am     Reply with quote

@dluu13 - great points. The +=1 notation more clearly shows intended behaviour, so I used that and it works correctly.

Code:
Count[ID] += 1; // This works


@Ttelmah - I had a feeling you would know what was going on. Thanks for clarifying the history through compiler versions.

Hopefully this can help someone else!
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