08-16-2011 06:48 PM
I have inherited a CVI project that contains a single .C file and a couple of .UIR files with their associated .H files.
Many of the global variables have STATIC in their declaration - I believe this is due to the fact the original developer originally had them inside various functions and at some point changed strategy to make them global, leaving the STATIC keyword in place when copy/pasting.
As I understand it the ONLY implication of STATIC on a global variable would be to keep its visibility only to the .C file in which it is declared. In this instance that is moot since there is only one .C file in the project.
Is there anything about LabWindows/CVI which I might not be aware of that makes this use of STATIC cause something different than if STATIC were not used?
Thanks for any input you might have since I'm new to CVI 🙂
08-17-2011 07:38 AM
Roberto and menchar said it well:
"static variables are declared at compile time and survive when their block of code terminates. If declared at function level, their value survives from one call of the function to another, so that they can be used over time to store permanent values locale to the function. If declared at module level, they are common to all functions in the module and are allocated outside the stack so that their values are not lost during program life. In every case they can be accessed and modified in values by functions in the program. If arrays, they cannot be dinamically changed in size."
"The static keyword can be used with a function name also, meaning the function name isn't exported to the linker, and the function is in scope only to code within the module."
08-17-2011 02:02 PM
Another aspect of static variables in C is that you're guaranteed that the initial value of the variable, as it's first allocated off the heap, is zero - the C runtime does this for you prior to starting execution of your process. This is unlike automatic variables allocated off the stack which are not initialized automatically.