🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Doug Schmidt Sun, 22 Oct 1989 01:10
static initialization
#524
Author: Doug Schmidt
Date: Sun, 22 Oct 1989 01:10
35 lines
1473 bytes
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
Re: static initialization
#540
Author: mx!cbcscmrs
Date: Tue, 24 Oct 1989 03:35
49 lines
2149 bytes
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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads