View previous topic :: View next topic |
Author |
Message |
edmundopt
Joined: 08 Dec 2011 Posts: 15 Location: Portugal
|
memory initialization |
Posted: Sat Jan 07, 2012 1:34 pm |
|
|
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
|
|
Posted: Sat Jan 07, 2012 2:47 pm |
|
|
Try
Code: | unsigned int8 Spi_buffer [16] = {0} |
to fill the Array with zero.
Best regards
Nilsener |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Sat Jan 07, 2012 3:26 pm |
|
|
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
|
|
Posted: Sat Jan 07, 2012 4:38 pm |
|
|
thank you, the #ZERO_RAM works like a charm
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: 19536
|
|
Posted: Sun Jan 08, 2012 2:50 am |
|
|
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 |
|
|
|