[Icc-avr] Module globals static?
John Baraclough
j_baraclough at zetnet.co.uk
Wed Oct 24 06:24:12 PDT 2007
At 10:45 24/10/2007, you wrote:
>Bengt Ragnemalm wrote:
>>Is a variable that is global in a module also automatically static?
>>I think it is but if so, shouldn't the strictly most correct way to
>>define the globals also be as static?
>>
>
>Yes, global variables are statically allocated, meaning they have a
>fixed address in memory (assigned by the linker) and exist
>throughout the lifetime of the program.
>
>You can think of the keyword "static" as having two distinct effects
>on variables - it ensures that they have a statically allocated
>address, and it ensures that they have the minimum possible
>scope. Thus all file-level data and functions should be declared
>"static" unless you are specifically defining global data or
>functions that should be available from other files.
>
>mvh.,
>
>David
Sometimes you need to be careful, as the file scope limiting effect
of the static keyword can be dangerous. A variable declared as static
in one file can have an identical name to a static variable in
another file, but not be at the same address. The linker will know
the difference but the programmer may not, especially if they are
maintaining someone else's code.
HTH
John
More information about the Icc-avr
mailing list