|
|
View previous topic :: View next topic |
Author |
Message |
don Guest
|
How do I disable initialization of static variables? |
Posted: Mon Dec 17, 2007 10:31 pm |
|
|
Hi,
Immediately after main() in the LST file there are lots of CLRF instructions where static variables are being cleared. I don't need to have the static variables set to zero. Is there a way to disable this feature?
Thanks. |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 18, 2007 3:55 am |
|
|
Different answers according to what you really need.
First, if you want the variables initialised, but not to zero, then declare them with their values. So:
static int8 fred=100;
Which will make the compiler initialise them to your required value. Given that values must be initialised somewhere, this is a good way to deal with static values.
Second, remember that any variables declared in the 'main', inherently _are_ static already (only variables in subroutines are 're-used').
Third, if you must have static variables, and want them to retain their value on a warm boot (this is great if you are using 'restart_cause', and want values to not be reinitialised), then instead use the #locate declaration for the variable, instead of the 'static'declaration. This physically forces a value to be placed at a location in memory, that _you_ chose, and C will not initialise the area concerned. The value will be static, but not initilised by the compiler. This is how I declare 'static' values on code designed to handle a watchdog restart.
Best Wishes |
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Tue Dec 18, 2007 6:10 pm |
|
|
I can't remember how I found this out, but for static variables that are declared outside of the main or any subroutines, just omitting the word 'static' keeps the compiler from generating the code to initialize to 0.
Code: |
static int8 tenthsval; //used for holding next number to display
static int8 onesval;
static int8 tensval;
int8 minsval;
int8 tminsval;
|
--- Listing ---
Code: |
.................... void main()
.................... {
[ ... ]
36DA: CLRF 05
36DC: CLRF 06
36DE: CLRF 07
[ other variables not cleared]
|
--- Symbol Table ---
Code: |
005 tenthsval
006 onesval
007 tensval
008 minsval
009 tminsval
|
|
|
|
Ttelmah Guest
|
|
Posted: Fri Dec 21, 2007 4:50 am |
|
|
If you declare variables outside the 'main', they become _global_ variables. These are inherently 'static', but because they are not declared as 'static', they are not zeroed. Difference is (of course), that they are accessible from anywhere. The use of 'static' variables, is normally to have a variable in a subroutine, that is 'distinct' from those externally.
As I said, it depends what you actually 'want'...
Best Wishes |
|
|
|
|
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
|