Atmel CAVR-4 Manuale Utente
CAVR-4
Part1. Using the compiler
Efficient coding for embedded applications
131
SAVING STACK SPACE AND RAM MEMORY
The following is a list of programming techniques that will, when followed, save
memory and stack space:
memory and stack space:
●
If stack space is limited, avoid long call chains and recursive functions.
●
Avoid using large non-scalar types, such as structures, as parameters or return type;
in order to save stack space, you should instead pass them as pointers or, in C++, as
references.
in order to save stack space, you should instead pass them as pointers or, in C++, as
references.
FUNCTION PROTOTYPES
It is possible to declare and define functions using one of two different styles:
●
Prototyped
●
Kernighan & Ritchie C (K&R C)
Both styles are included in the C standard; however, it is recommended to use the
prototyped style, since it makes it easier for the compiler to find problems in the code.
In addition, using the prototyped style will make it possible to generate more efficient
code, since type promotion (implicit casting) is not needed. The K&R style is only
supported for compatibility reasons.
prototyped style, since it makes it easier for the compiler to find problems in the code.
In addition, using the prototyped style will make it possible to generate more efficient
code, since type promotion (implicit casting) is not needed. The K&R style is only
supported for compatibility reasons.
To make the compiler verify that all functions have proper prototypes, use the compiler
option Require prototypes (
option Require prototypes (
--require_prototypes
).
Prototyped style
In prototyped function declarations, the type for each parameter must be specified.
int test(char, int);
/* declaration */
int test(char a, int b)
/* definition */
{
.....
}
Kernighan & Ritchie style
In K&R style—traditional pre-ISO/ANSI C—it is not possible to declare a function
prototyped. Instead, an empty parameter list is used in the function declaration. Also,
the definition looks different.
prototyped. Instead, an empty parameter list is used in the function declaration. Also,
the definition looks different.
int test();
/* old declaration */
int test(a,b)
/* old definition */
char a;
int b;
{
.....
}