🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #2163

hard bug

#2163
From: Micheal Tiemann
Date: Thu, 11 Jan 1990 19:53
37 lines
1162 bytes
Here's a C++ teaser.  What's wrong with the following code?

    extern "C" void printf (char *, ...);
    struct X
    {
      int x;

      X (int i) { x = i; }
      ~X () { printf ("destroying object %d\n", x); }
    };

    main ()
    {
      int i = foo ();

      if (i)
	for (X x(5); i < 10; i++)
	  {
	    printf ("loop count = %d\n", i);
	  }
      Greyhawk:
	;
    }

    int foo () { return 0; }

More precisely, when execution reaches Greyhawk,

	1.  Is `x' in scope?
	2.  If so, it is initialized?
	3.  Where and when is `x' destroyed?

If the scope of `x' were limited to the scope of the for loop, none of
these problems would be "interesting", and everything work work as one
might expect.

Michael
Technical Details
Message-ID: <9001111953.AA13433@apple-gunkies.ai.mit.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ai.mit.edu!tiemann

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ai.mit.edu!tiemann
From: tiemann@ai.mit.edu (Micheal Tiemann)
Newsgroups: gnu.g++.bug
Subject: hard bug
Message-ID: <9001111953.AA13433@apple-gunkies.ai.mit.edu>
Date: 11 Jan 90 19:53:07 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 37