[Icc-avr] Are 'local' vars in main automatuc or in bss?

David Brown david_brown at hotpop.com
Fri Oct 26 01:28:04 PDT 2007


Graham Whyte wrote:
> main() is no different from any other subroutine except that it gets 
> called at startup
>  
> Variables declared outside subroutines or functions in any module file 
> are Global within that module
> The can be referenced from anywhere within that module

Correct up to here...

> To make them Global to other modules, each other module needs to declare 
> then as External

That's wrong, and very misleading.

Variables (and functions) get global linkage if they are defined at file 
scope without "static" - C defaults to making them global and externally 
accessible.  Thus two "int x;" definitions at file scope in two modules 
refer to the same variable at the same address, regardless of any 
"extern" declarations.

The point of "extern" is to tell the compiler about a global variable 
(or function) without defining it - it allows access to a global 
variable, but does not turn a variable into a global one.

> This is exactly the same as making a subroutine available to other modules
> The declarations must of course be identical
>  

The declaration *should* be identical - but be careful, because C allows 
you to get it completely wrong and still be legal C.

> You can make this very neat and tidy by putting the Globals in a 
> separate module
> Then create the external  declarations in a separate header file which 
> you can #include wherever you need
> You then only got one copy to worry about

I've seen programs made like this - as Ton says, "Yuck!".

If you want to write programs that are readable, understandable, 
debugable and maintainable, and allow for at least some level of re-use 
between programs, then you will avoid this setup like the plague.

Modularisation is critical to writing software.  Break your program into 
parts that fit logically together, and figure out what data and 
functions (and types, macros, etc.) that module makes available to other 
parts of the whole program.  This information is your module's 
interface, or API, and forms its "module.h" header file.

The header file "module.h" contains comments about how to use the 
module, along with all type declarations, #define's, "extern" 
declarations of data and functions, and any #include's that are needed 
to *use* the module (for example, if it contains a line "extern uint8_t 
count;", then it should also contain a line "#include <stdint.h>" or 
equivalent).

The C file "module.c" contains the *implementation* of the module, and 
has #include "module." near the beginning.  Every file-scope variable or 
function is either private to the module, and defined "static", *or* it 
has a corresponding "extern" declaration in "module.h".  There should be 
no exceptions (except perhaps temporarily to aid debugging, since you 
can see non-static items in the map file).


As I said in an earlier post, I tend to rant a bit and treat my methods 
as the only correct methods, but in this case there is no real doubt. 
If you look at any other properly structured programming language, such 
as Pascal, Modula 2, or Ada, you'll see that this is exactly the way 
good modular programming is always done - other languages enforce these 
rules.

mvh.,

David


>  
> Graham
>  
> -----Original Message-----
> *From:* icc-avr-bounces at imagecraft.com 
> [mailto:icc-avr-bounces at imagecraft.com]*On Behalf Of *bobgardner at aol.com
> *Sent:* 25 October 2007 14:30
> *To:* icc-avr at imagecraft.com
> *Subject:* [Icc-avr] Are 'local' vars in main automatuc or in bss?
> 
> Great summary of 'static' vars.... I remember in pascal the scope 
> 'nested' and stuff 'up a level' was visible, but c doesnt allow nested 
> functions I guess. Just to clarify, if I declare vars in main, are they 
> local or in the bss? Is main a subroutine that is called with params and 
> returns a value? Is the iccavr microcontroller model different from a 
> ram based system with an os?
> ------------------------------------------------------------------------
> Email and AIM finally together. You've gotta check out free AOL Mail 
> <http://o.aolcdn.com/cdn.webmail.aol.com/mailtour/aol/en-us/index.htm?ncid=AOLAOF00020000000970>!
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr



More information about the Icc-avr mailing list