[gnu.g++.bug] static initialization

schmidt@glacier.ics.uci.edu (Doug Schmidt) (10/22/89)

If you want some fun, try guessing what the `correct' output should be
for the following C++ code accepted without warning from G++.  Then
compile and execute it with G++ and check your work.

----------------------------------------
#include <stdio.h>

int *frob(int k)
{
  static int N[2] = {k, k};
  return N;
}

main ()
{
  int i;
  
  for (i = 5; i < 10; i++)
    printf ("i = %d\n", frob(i)[1]);
}
----------------------------------------

cfront 2.0 complains:

"yow.c", line 13: sorry, not implemented: local name k in initializer for static

I think I like the G++ solution, although it is not clear exactly what
the `official' output should be.

Doug
--
Master Swordsman speak of humility;             | schmidt@ics.uci.edu (ARPA)
Philosophers speak of truth;                    | office: (714) 856-4034
Saints and wisemen speak of the Tao of no doubt;
The moon, sun, and sea speaks for itself. -- Hiroshi Hamada

cbcscmrs@csun.edu (10/24/89)

In article <1989Oct21.181007.10849@paris.ics.uci.edu> schmidt@glacier.ics.uci.edu (Doug Schmidt) writes:
+----
| If you want some fun, try guessing what the `correct' output should be
| for the following C++ code accepted without warning from G++.  Then
| compile and execute it with G++ and check your work.
| 
| ----------------------------------------
| #include <stdio.h>
| 
| int *frob(int k)
| {
|   static int N[2] = {k, k};
|   return N;
| }
| 
| main ()
| {
|   int i;
|   
|   for (i = 5; i < 10; i++)
|     printf ("i = %d\n", frob(i)[1]);
| }
| ----------------------------------------
| 
| cfront 2.0 complains:
| 
| "yow.c", line 13: sorry, not implemented: local name k in initializer for static
| 
| I think I like the G++ solution, although it is not clear exactly what
| the `official' output should be.
+----

Well, I just check it, and I looked at the semantics embodied in the assembly
code it generated.  It does what I expected it too.  (Going on the assumption
that it would generate code.)  I think for what it does, it does it well.

The only gripe that I can see is the gripe about a warning message to the
effect: ``warning: only first initialization will be used to initialize
object.''

Others?  How do you feel?

Spoiler follows:

For those that don't have g++-1.36 (or later) the program outputs a bunch
of 5s.  The code that is generated allocates an unnamed temporary initialized
to zero, and when the function is called and the statics initialized, the
temporary is changed to one.  It only initializes the static object if the
temporary is zero.