A89: Re: Just subscribed... my first question


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

A89: Re: Just subscribed... my first question




Hi!

I will elaborate why "static" initialized arrays may make program.
Look this, for example:

void fnc(void)
{
 int a[10]={15,12,8,6,14,13,7,9,2,5};
 ...
}

In above case, array "a" is "auto", so it need to be allocated
on the stack frame (A6 usually points to the stack frame). But,
as it is initialized, the initialization need to be done in a 
run time, because auto variables are allocated whenever the 
function "fnc" is called, and destroyed whenever the "fnc"
finishes. If the array is small, it can be done for example
by using a construction like

move.w #15,(%a6,-30)
move.w #12,(%a6,-28)
move.w #8,(%a6,-26)
...

etc. If the array is big, the compiler will store initializer 
sequence (15,12,8,... in above example) somewhere in the executable
file (.89z on TI-89), and will call "bcopy" routine which will 
copy this sequence to the space allocated on the stack. This
need to be done at the begining on "fnc". As this will be performed
each time "fnc" is called, the array will always be initilalized
again, so eventual changes in array data will not survive after
the function finishes.

Now, look this example:

void fnc(void)
{
 static int a[10]={15,12,8,6,14,13,7,9,2,5};
 ...
}

Array "a" is now static, so it will survive after the end of
function "fnc". Such array will retain it's value  across 
function calls, so the initialization need to be performed
just once. The initialization in fact will be performed just
at the compile time: the compiler will embed the sequence
15,12,8,6,14,13,7,9,2,5 in the executable file, and 
declaration like static int a[10]=... will just make that
"a" points to this allocated area. This is surely shorter 
than initializing the array in run time (e.g. copying to the
stack frame). Clear?

So, if the array hold constant data which will not be
modified, static allocation has an advantage...

Note that usage of non-array static variables may even have an
opposite effect: increasing of program size, because static
data require additional entries in the program relocation table.
I don't have time at the moment to elaborate this in more details.

Zeljko Juric

> I read in one of the archives that using static variables in TI-GCC makes
> the program smaller. Could someone explain this? As far as I know, static
> has 2 meanings in C:
> 
> 1. When used to modify a local variable, specifies that the variable is
to
> have static duration. This means it is allocated when the program begins
and
> deallocated when the program finishes. A variable declared as static in a
> function will retain it's value across function calls:
> 
> int inc(void)
> {
>     static int i = 0; /*initialization occurs on the first call only*/
>     return ++i;
> }
> 
> inc will return 1 the first time it is called, 2 the second time it is
> called, and so on.
> 
> Variables that are not declared with a specific storage class modifier
> (static, register, extern) are assumed to be auto. auto declared
variables
> are allocated when program execution reaches the block in which they are
> declared and deallocated when that block finishes.
> 
> 2. When used to modify global variables, static specifies that the
variable
> has file scope and exists only in the file in which it is declared. This
is
> not useful in TI-GCC, since mutiple source files is not supported.
> 
> So basically, I don't understand how static, as opposed to auto could
affect
> the file size. Can anyone elaborate?
> 
> -Kevin
>