[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Register and static variables

From: David Smead
Subject: Re: [avr-gcc-list] Register and static variables
Date: Sat, 23 Mar 2002 11:43:05 -0800 (PST)

A global variable is known throughout the compile/link set of files.

A static variable is known only to the functions in the file where the
static is declared.

Global variables are a good way to shoot yourself in the foot, and an
indication that the design isn't the best.  On the otherhand, with limited
RAM, globals can make optimum use of precious resources.

I always keep all global declarations in one file that is included by all
other files that need it.  You need to ifdef things so than when main
includes it the variable is declared, but all other includes see it as
extern.  For example:

#ifndef GLOBAL_H
#define GLOBAL_H

#ifdef EXTERN
#undef EXTERN

#ifdef MAIN
#define EXTERN
#define EXTERN extern
#endif          // MAIN

EXTERN int current_loc;  // declare/reference a global
#endif          // GLOBAL_H

In main.c declare the symbol MAIN.  In all other files don't define it.

If you want to declare and initialize a global variable this scheme
presents a problem that can be solved by putting the declaration in twice
within the MAIN ifdef.  A better solution is to have a section in your
start-up code that explicitly initializes globals.


David Smead

On Sat, 23 Mar 2002, Paulo Abreu wrote:

> What is the diff between declaring a 'normal' global variable and a
> static global variable?
> Is it possible to declare a global variable, but forcing it to be in
> registers instead SRAM?
> Thank you for your attention
> Paulo Abreu
> avr-gcc-list at http://avr1.org

avr-gcc-list at http://avr1.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]