[comp.lang.c] array init'ing, static or auto?

yahnke@vms.macc.wisc.edu (Ross Yahnke, MACC) (11/12/89)

If I init a char array declared inside a function with a string:
  wubba()
  { char wubbaStr[] = "wubba";
  ...

when does actually get init'ed? When the function is called?

If wubba() got called a few thousand times, would it be faster
to make wubbaStr static instead?
  wubba()
  { static char wubbaStr[] = "wubba";

I'm assuming it would be cuz wubbaStr would get init'ed at program
load time, and not when the function is called... any comments?

>>>      Internet: yahnke@macc.wisc.edu        <<<
>>>   Mille voix chuchottent <<c'est vrai>>    <<<

cpcahil@virtech.uucp (Conor P. Cahill) (11/12/89)

In article <2679@dogie.macc.wisc.edu>, yahnke@vms.macc.wisc.edu (Ross Yahnke, MACC) writes:
> If I init a char array declared inside a function with a string:
>   wubba()
>   { char wubbaStr[] = "wubba";
>   ...
> when does actually get init'ed? When the function is called?

pcc gives an error about "no agregate initializations..."
gcc compiles it and initializes the data for each call to the function.
If your compiler accepts it, it will probably initialize it for each
iteration of the function.

> If wubba() got called a few thousand times, would it be faster
> to make wubbaStr static instead?
>   wubba()
>   { static char wubbaStr[] = "wubba";

Yes, if you don't change wubbaStr[].  However if you change wubbaStr[] the
string will not be reset to it's original value the next time through.


-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/14/89)

In article <2679@dogie.macc.wisc.edu> yahnke@vms.macc.wisc.edu (Ross Yahnke, MACC) writes:
>  wubba()
>  { char wubbaStr[] = "wubba";
>when does actually get init'ed? When the function is called?

This is an attempted initialization of an auto aggregate,
which is not supported by many C implementations.

>If wubba() got called a few thousand times, would it be faster
>to make wubbaStr static instead?
>  wubba()
>  { static char wubbaStr[] = "wubba";
>I'm assuming it would be cuz wubbaStr would get init'ed at program
>load time, and not when the function is called... any comments?

To avoid implementation dependencies, let's recast the question
in terms of portable C:

wubba(){ int wubbaInt = 42;
	vs.
wubba(){ static int wubbaInt = 42;

The "static" case initializes the variable just once,
before the program starts to execute (perhaps at link time).
The "auto" case (i.e. the non-"static" one) initializes
the variable each time the block (function body) is entered.

There are advantages and drawbacks to both methods.
Static initialization is probably less computationally expensive,
but if you change the variable the change will "stick", so that
the next time the block is entered the NEW value will be in effect.
Sometimes that is exactly what you want, but more often it isn't.

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (11/15/89)

  If you are not going to modify the char array, by all means make it
static. Many compilers will not allow you initialize the array unless it
*is* static, although ANSI compilers should. You might want to init to a
char pointer, as this takes less time in init, may take more in
execution depending on what you do with it;

foo() {
  char str1[] = "abcd";		/* make not work on old compilers	*/
  static char str2[] = "abcd";	/* should be portable			*/
  char *str3 = "abcd";		/* should be portable			*/

  /* code here */
}

  Note that these are NOT completely the same, although most uses with
or without subscripts should generate the same results. There are
performance tradeoffs and if you declare an array instead of a pointer
you can't modify it (obviously).
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon