Atmel CAVR-4 ユーザーズマニュアル

ページ / 323
CAVR-4
Part 1. Using the compiler
Data storage
23
Example
In the example below, an object, named 
delta
, of the type 
MyClass
 is defined in data16 
memory. The class contains a static member variable that is stored in data20 memory.
// The class declaration (placed in a header file):
class MyClass
{
public:
  int alpha;
  int beta;
  _ _eeprom static int gamma;
};
// Definitions needed (should be placed in a source file):
_ _eeprom int MyClass::gamma;
// A variable definition:
MyClass delta;
The stack and auto variables
Variables that are defined inside a function—not declared static—are named auto 
variables
 by the C standard. A small number of these variables are placed in processor 
registers; the rest are placed on the stack. From a semantic point of view, this is 
equivalent. The main differences are that accessing registers is faster, and that less 
memory is required compared to when variables are located on the stack. 
Auto variables live as long as the function executes; when the function returns, the 
memory allocated on the stack is released.
The stack can contain:
Local variables and parameters not stored in registers
Temporary results of expressions
The return value of a function (unless it is passed in registers)
Processor state during interrupts
Processor registers that should be restored before the function returns (callee-save 
registers).