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

inialize variables at declaration or later?

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



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

inialize variables at declaration or later?
PostPosted: Fri May 13, 2005 3:06 pm     Reply with quote

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.

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

PostPosted: Fri May 13, 2005 3:57 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 13, 2005 4:32 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri May 13, 2005 8:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 14, 2005 5:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 14, 2005 7:13 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sat May 14, 2005 7:39 am     Reply with quote

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







PostPosted: Sat May 14, 2005 9:32 am     Reply with quote

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