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

Array take values of other variables

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



Joined: 25 Feb 2013
Posts: 6

View user's profile Send private message

Array take values of other variables
PostPosted: Mon Feb 25, 2013 12:49 pm     Reply with quote

I'm programming a 16f877a.h ... and I have in my program arrays ... at some point take values of other variables and I do not know why? Someone has this happened? please help
Ttelmah



Joined: 11 Mar 2010
Posts: 19384

View user's profile Send private message

PostPosted: Mon Feb 25, 2013 1:14 pm     Reply with quote

It is happening, because you are writing beyond the end of an array somewhere.
Arrays are not 'bounds checked' (the hardware does not support this). It is up to _you_ to be careful. So (for instance):
Code:


int8 chars[10]
int16 something[4];
int8 anumber=50;

void some_code(void) {
   int8 ctr;
   for (ctr=1;ctr<=10;ctr++)
      chars[ctr]=anumber;
}
//at this point 'something[0]' will probably have been overwritten

The problem here is that in C arrays are zero referenced. So chars has entries from 0 to 9. I then write to entries 1 to 10, and since (generally) variables are stored in memory in the order they are declared, chars[10], overwrites something[0].....

Common causes of problems are:
1) Forgetting arrays are zero referenced.
2) Forgetting strings need space for the terminating null.
3) Just general errors....

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 25, 2013 1:17 pm     Reply with quote

Read these threads. See if you are doing anything mentioned in them.

variables contain bad values -
http://www.ccsinfo.com/forum/viewtopic.php?t=46536
Simple source code = Erratic behaviour -
http://www.ccsinfo.com/forum/viewtopic.php?t=41964
possible processor problem -
http://www.ccsinfo.com/forum/viewtopic.php?t=36556
Program doesn't function after meaningless changes to code -
http://www.ccsinfo.com/forum/viewtopic.php?t=31335
leojma



Joined: 25 Feb 2013
Posts: 6

View user's profile Send private message

PostPosted: Mon Feb 25, 2013 8:35 pm     Reply with quote

ok thank you very much for your help problem solved! Very Happy
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