SV: [Icc-avr] Module globals static?

David Brown david_brown at hotpop.com
Wed Oct 24 07:30:02 PDT 2007


Bengt Ragnemalm wrote:
> Do you mean that there is a difference between declaring a global variable
> with and without the static operand?
> 

Yes - this is C programming basics.  If you don't know this stuff, you 
should be reading a good C programming book (or at least some web 
tutorials).

> Is there in this matter any difference in a global variable defined in the
> same file as main and a global variable in another file? (I call these
> module global). I know that a variable that is defined global in another
> file will only be seen by functions in that file.
> 

A variable that can only be seen in the file in which it is defined is, 
by definition, not a global variable.  It is a file-local variable, and 
you achieve this by defining it as "static".

> The way you write make me think that if I write static to a file global
> variable will not make it totally global but not static will.
> 

That's a slightly confusing sentence - but if I've interpreted it 
correctly, then you are correct.

There are two separate concepts to consider here.  There is the lifetime 
of the variables, and there is the visibility (or "scope") of the 
symbols for accessing the data.  It is also important to understand the 
difference between a "definition" and a "declaration".  Let's first look 
at the three choices for lifetimes for variables:

1) Local variables.  These are allocated on the run-time stack, in 
registers, or may even be optimised away by the compiler.  They are 
defined locally in a function, without "static".  They are not 
initialised by default, and they disappear when they go out of scope.

2) Statically allocated variables.  These have a specific memory 
address, and exist from just before main() begins.  They can be either 
explicitly initialised, or will default to 0.  They are defined at file 
scope (with or without "static") or as function-local "static" variables.

3) Heap allocated data.  This is data allocated using malloc/free (or a 
similar dynamic memory system).  They exist from when they are 
malloc'ed, until they are free'ed.  These are not directly accessible, 
but must be accessed through pointers of some kind.


For scope, you can have local scope (inside a function, or a block 
within a function - regardless of "static" modifiers), file scope 
(file-level definitions with "static") and global scope (file-level 
definitions without "static", or "extern" declarations).  It is good 
programming practice to always use the smallest possible scope for your 
data and functions.


A "definition" says what a variable or function is, and causes the 
compiler to allocate memory for storage.  A "declaration" merely tells 
the compiler about the variable (or function), but does not allocate any 
memory.  In particular, "extern" statements are declarations but not 
definitions, while all definitions are also declarations.  Every item of 
data should have a single definition within the program.  Global data 
will also need an "extern" declaration to be accessible from other 
modules.  The only sane way to handle this is that for any module 
"module.c", every function and variable is either declared "static" 
(which should be the default, but the C language designers were lazy), 
or has a corresponding "extern" declaration in "module.h".  The file 
"module.c", and every module that needs access to module's globals, must 
have #include "module.h".


Does that terse summary help?

mvh.,

David




> /Bengt
> 
>> -----Ursprungligt meddelande-----
>> Från: icc-avr-bounces at imagecraft.com [mailto:icc-avr-
>> bounces at imagecraft.com] För David Brown
>> Skickat: den 24 oktober 2007 11:45
>> Till: Discussion list for ICCAVR and ICCtiny Users. You do NOT need
>> tosubscribe to icc-announce if you are a member of this.
>> Ämne: Re: [Icc-avr] Module globals static?
>>
>> 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
>>



More information about the Icc-avr mailing list