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

memory initialization

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



Joined: 08 Dec 2011
Posts: 15
Location: Portugal

View user's profile Send private message

memory initialization
PostPosted: Sat Jan 07, 2012 1:34 pm     Reply with quote

Hello all

A question, when declared an array inside main, such as
Code:

unsigned int8 spi_buffer[16];

is it normal that, when printing it's contents for the first time, values different from zero appear ?
Code:

main
{
 unsigned int8 int8_tmp;
//...
 for (int8_tmp=0;int8_tmp<16;int8_tmp++)
 {
   printf(" :%x",spi_buffer[int8_tmp]);
 }

ccs version 4.124, pic18f4620 or pic16f877a.

Note, after filling the array with values, is stores ok and updates value ok.
nilsener



Joined: 06 Dec 2005
Posts: 59

View user's profile Send private message

PostPosted: Sat Jan 07, 2012 2:47 pm     Reply with quote

Try

Code:
unsigned int8 Spi_buffer [16] = {0}


to fill the Array with zero.

Best regards
Nilsener
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Sat Jan 07, 2012 3:26 pm     Reply with quote

and, 'yes' it is normal.
'static' variables are initialised to zero.

If you use '#zero_ram', then all variables are cleared to zero, but at a significant cost in time during boot.

Otherwise though variables contain whatever they are explicitly initialised to, _or_ if not initialised, what the memory cells 'happen' to contain. This is actually useful, since it means if a variable is not initialised, and the chip does a watchdog restart, the variable values will contain what they previously contained.
Hence you can test the 'restart_cause', and if this is a watchdog restart, boot straight up _without_ initialising the variables, while for all other restart causes, call an 'init' routine, to write the values you want into place.

Best Wishes
edmundopt



Joined: 08 Dec 2011
Posts: 15
Location: Portugal

View user's profile Send private message

PostPosted: Sat Jan 07, 2012 4:38 pm     Reply with quote

thank you, the #ZERO_RAM works like a charm Smile

That was the source of many weird behaviour, because previous values were inside the variables, sometimes all went ok, othertimes complete erratic behavior, the fault is allways of the programmer eheheh

have a nice day
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Sun Jan 08, 2012 2:50 am     Reply with quote

Seriously though, this is a sign of bad programming.
You shouldn't rely on zero_ram, instead _explicitly_ initialise your variables to the values they want. zero_ram is a bodge for people who are not programming well....
(Evil grin).

Best Wishes
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