|
|
View previous topic :: View next topic |
Author |
Message |
valemike Guest
|
keyword "static" |
Posted: Tue Jun 01, 2004 1:45 pm |
|
|
According to the manual, the keyword "static" is
Code: |
Variable is globally active and initialized to 0
|
The brief description leads me to conclude that, even if i use static inside a function, then it is globally active, and thus, I shouldn't use the same name (in this case, "initialized") more than once.
However I have used "static" several times INSIDE a function, without problems.
e.g.
Code: |
int fxn1(void)
{
static int initialized;
....
}
int fxn2(void
{
static int initialized;
....
}
...
|
It seems to work just fine, contrary to how I interpret the description in the CCS manual. Each instance of "initialized" retains its value even after exiting and re-entering the functions. The CCS manual leads me to interpret that "initialized" is global, and thus cannot be used again.
Even though it seems to work fine, is this proper usage of static in the CCS environment
Furthermore, I really see no use for using "static" outside a function, since we cannot have multiple .c files compiling separately anyways.
-Mike
[/code] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah Guest
|
Re: keyword "static" |
Posted: Tue Jun 01, 2004 2:53 pm |
|
|
valemike wrote: | According to the manual, the keyword "static" is
Code: |
Variable is globally active and initialized to 0
|
The brief description leads me to conclude that, even if i use static inside a function, then it is globally active, and thus, I shouldn't use the same name (in this case, "initialized") more than once.
However I have used "static" several times INSIDE a function, without problems.
e.g.
Code: |
int fxn1(void)
{
static int initialized;
....
}
int fxn2(void
{
static int initialized;
....
}
...
|
It seems to work just fine, contrary to how I interpret the description in the CCS manual. Each instance of "initialized" retains its value even after exiting and re-entering the functions. The CCS manual leads me to interpret that "initialized" is global, and thus cannot be used again.
Even though it seems to work fine, is this proper usage of static in the CCS environment
Furthermore, I really see no use for using "static" outside a function, since we cannot have multiple .c files compiling separately anyways.
-Mike
[/code] |
A variable declared inside a function, gets the name of the function appended to the front of it's declaration. So if you define a 'fred' inside 'main', and then another inside a subroutine, the two variables still have different names, and will not refer to the same storage area.
Have a look at the symbol declaration file to see how this is done.
Static used outside the functions, is a way of declaring a global variable, that is initialised to zero, without having to declare as:
int8 fred=0;
or use the 'zero RAM' declaration. The disadvantage of the latter, is that on larger chips, it takes a long time, given the amount of memory that can be involved.
Otherwise in the CCS C, it offers no real advantage.
Best Wishes |
|
|
Devin Electronics Guest
|
Global Variable VS Global Lifetime within subroutine |
Posted: Thu Sep 15, 2005 1:07 pm |
|
|
Thanks for the link to the MS explanation. Now it makes since why my code doesn't work the way I thought it would.
STATIC: A variable declared at the internal level with the static storage-class specifier has a global lifetimebut is visible only within the block in which it is declared.
But maybe someone has a way to work around the problem. Trying to NOT declare my "message" variable in the main C code. For critical timing reason, I cannot rotate "message" within the transmit_message routine.
Psudocode as follows:
void transmit_1(void) {
subroutine to toggle two pins with a one frequency
rotate_left(&message+0,1);
//bit-bang some pins
rotate_left(&message+1,1);
//bit-bang some pins
rotate_left(&message+2,1);
//bit-bang some pins
rotate_left(&message+3,1); //Rotate MSB into CARRY
//bit-bang some pins
}
void transmit_0(void){
subroutine to toggle two pins at a different frequency
rotate_left(&message+0,1);
//bit-bang some pins
rotate_left(&message+1,1);
//bit-bang some pins
rotate_left(&message+2,1);
//bit-bang some pins
rotate_left(&message+3,1); //Rotate MSB into CARRY
//bit-bang some pins}
void transmit_message (int32 msg) {
int8 count;
static32 int32 message;
for (count=0; count<=31;count++) { //loop through message
if (STATUS.0 == 0)
transmit_1();
else
transmit_0();
}
} |
|
|
Guest
|
|
Posted: Thu Sep 15, 2005 1:19 pm |
|
|
Never mind. Found the solution. Code mentioned was part of an #include file. Found another post that mentioned just putting the "int32 message" declaration at the top line of the #include file outside of any routines. Works. |
|
|
|
|
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
|