User`s manual

Dynamic C Users Manual digi.com 45
4.20 Global Initialization
Various hardware devices in a system need to be initialized, not only by setting variables and control regis-
ters, but often by complex initialization procedures. Dynamic C provides a specific function chain,
_GLOBAL_INIT, for this purpose. Your program can add segments to the _GLOBAL_INIT function
chain, as shown in the example below.
The special directive #GLOBAL_INIT{ } tells the compiler to add the code in the block enclosed in
braces to the _GLOBAL_INIT function chain. Any number of #GLOBAL_INIT
sections may be used in
your code. The order in which they are called is indeterminate since it depends on the order in which they
were compiled. The storage class for variables used in a global initialization section must be static. Since
the default storage class is auto, you must define variables as static in your application.
The _GLOBAL_INIT function chain is always called when your program starts up, so there is nothing
special to do to invoke it. In addition, it may be called explicitly at any time in an application program with
the statement:
_GLOBAL_INIT();
Make this call this with caution. All costatements and cofunctions will be initialized. See Section 7.2 for
more information about calling _GLOBAL_INIT().
long my_func( char j );
main(){
my_func(100);
}
long my_func(char j){
static int i;
static long array[256];
// The GLOBAL_INIT section is automatically run once when the program starts up
#GLOBAL_INIT{
for( i = 0; i < 100; i++ ){
array[i] = i*i;
}
}
return array[j]; // only this code runs when the function is called
}