View previous topic :: View next topic |
Author |
Message |
pfournier
Joined: 30 Sep 2003 Posts: 89
|
inialize variables at declaration or later? |
Posted: Fri May 13, 2005 3:06 pm |
|
|
For the PIC family, is there any advantage to initializing variable at the time you declare them over initializing them in the first part of code?
-Pete _________________ -Pete |
|
|
bfemmel
Joined: 18 Jul 2004 Posts: 40 Location: San Carlos, CA.
|
|
Posted: Fri May 13, 2005 3:57 pm |
|
|
Pete,
I don't think this is a "PIC family" quetion, it is up to the compiler to create the code. However, I tried two simple programs, one with initializing a variable when I declared it and one initializing the variable after the decalration as in:
Code: | int8 vara = 8;
// OR
int8 vara;
vara = 8; |
Both generated the same two assembler instructions:
000048 0E08 MOVLW 0x8
00004A 6E06 MOVWF 0x6, ACCESS
The only difference would be that one might be at the start of the code while one might be deep in the code somewhere but I don't see any speed or architecture advantages. I like the idea of initializing the variable where I use that since that is where I would look for it when debugging especially if it is a global variable.
- Bruce |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri May 13, 2005 4:32 pm |
|
|
CCS also implement the #zero_ram pre-processor directive that initialize to
zero (before program execution) all the RAM space that hold the declared variables.
Code: |
//Your header
................
................
#zero_ram
void main()
{
}
|
Compile your code with this and watch the listed output file.
Humberto |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri May 13, 2005 8:11 pm |
|
|
Humberto wrote: | CCS also implement the #zero_ram pre-processor directive that initialize to
zero (before program execution) all the RAM space that hold the declared variables.
Code: |
//Your header
................
................
#zero_ram
void main()
{
}
|
Compile your code with this and watch the listed output file.
Humberto |
Only problem with it is that on reset all your variables are now reset to zero. In some cases, this might not be desireable so make note of this in case you decide to use the zero_ram |
|
|
pfournier
Joined: 30 Sep 2003 Posts: 89
|
|
Posted: Sat May 14, 2005 5:49 am |
|
|
Thanks for the info. Very helpful I was trying to write some code that I could swap beween 2 processors (compilers) with as few changes as posssible. This helped me make a decision on how to handle initializations.
-Pete _________________ -Pete |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Sat May 14, 2005 7:13 am |
|
|
Mark wrote:
Quote: |
Only problem with it is that on reset all your variables are now reset to zero.
In some cases, this might not be desireable so make note of this in case you decide to use the zero_ram.
|
Of course that on reset I pretend that all the variables are initilized = 0.
Thatīs the reason for using zero_ram.
For those variables that I need to be initialized at value != 0 :
Code: |
// Header
................
................
#zero_ram
void main()
{
var_1 = 18;
var_2 = 68;
while(1)
{
.........
}
}
|
Humberto |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat May 14, 2005 7:39 am |
|
|
Humberto wrote: | Mark wrote:
Quote: |
Only problem with it is that on reset all your variables are now reset to zero.
In some cases, this might not be desireable so make note of this in case you decide to use the zero_ram.
|
Of course that on reset I pretend that all the variables are initilized = 0.
Thatīs the reason for using zero_ram.
For those variables that I need to be initialized at value != 0 :
Code: |
// Header
................
................
#zero_ram
void main()
{
var_1 = 18;
var_2 = 68;
while(1)
{
.........
}
}
|
Humberto |
You missed the point! I am referring to variables that have changed since their initialization. Say the watchdog resets the processor. Maybe you want to know the value of some of the variables. If you use the zero_ram then those values are history. Get it now? |
|
|
Ttelmah Guest
|
|
Posted: Sat May 14, 2005 9:32 am |
|
|
In fact this is a worse problem that that. All global variables are zeroed, even if #zero_ram is not used. I asked CCS a while ago, if they would change this, and make #zero_ram, have some options, so you could have '#zero_ram clearall' which would behave as it currently does, '#zero_ram noglobal', which would turn off the default clearing of global variables etc..
If you want a global variable to be maintained through a possible watchdog reset, you have to not as a normal variable, but put it into your own memory area.
There is a balancing act on setting varaibles. Setting takes time (as does #zero_ram), so only setting variables where you require an initial value, can save time, but brings with it the 'cost', that you must be careful to ensure that variables cannot be used before they are initialised.
Best Wishes |
|
|
|