[comp.lang.c++] Please help me...

eanv20@castle.ed.ac.uk (John Woods) (01/10/91)

	I would be very grateful for any help on the following.

"ARMtrace.h" contains the Tracer class from ARM 3.5 (p22). Objects
of this class are created with a string argument, which is echoed to the 
standard error in the form "... entered". On destruction of the object (when
it goes out of scope) the string is echoed once more, with the message "exit
from ...", providing a neat way to track the program. So, how does the
following output:

			main entered
				in main
			foo entered
				in foo, bar = 1
			exit from foo
				still in foo
				still in main
			exit from main
result from:

#include <stream.h>
#include "ARMtrace.h"

void foo( int bar ) {
	Tracer("foo");

	cerr << "\tin foo, bar = " << bar << "\n";
	if( bar ) ;
	cerr << "\tstill in foo\n";
}

main() {
	Tracer("main");

	cerr << "\tin main\n";
	foo(1);
	cerr << "\tstill in main\n";

	return 0;
}

	The interesting thing is that replacing if( bar ) with if( 1 ) or
if( 0 ) causes the output to be tthat expected.
	Sorry for the length and probable naivety of this posting, but I am 
at my wit's end. THadvanceNX...

				...John Woods


-- 
 /******* cut here ******* John Woods ******* cut here ********
 * Philosophy: Forsan et haec olim meminisse iuvabit (Virgil) *
 * Disclaimer: Every statement in this file is possibly !true *
 ******** cut here ******* John Woods ******* cut here *******/

eanv20@castle.ed.ac.uk (John Woods) (01/10/91)

Ahah. Could it be that only _named_ local objects may not be destructed before
they exit their scope? Must read my ARM more carefully. Sorry for wasting
everybody's bandwidth and time. However, this would render the example 
function void f() ARM 3.5 (p22) incorrect. Errata list, anyone?

-- 
 /******* cut here ******* John Woods ******* cut here ********
 * Philosophy: Forsan et haec olim meminisse iuvabit (Virgil) *
 * Disclaimer: Every statement in this file is possibly !true *
 ******** cut here ******* John Woods ******* cut here *******/

philip@pescadero.Stanford.EDU (Philip Machanick) (01/11/91)

In article <7749@castle.ed.ac.uk>, eanv20@castle.ed.ac.uk (John Woods) writes:
|> Ahah. Could it be that only _named_ local objects may not be destructed before
|> they exit their scope? Must read my ARM more carefully. Sorry for wasting
|> everybody's bandwidth and time. However, this would render the example 
|> function void f() ARM 3.5 (p22) incorrect. Errata list, anyone?

I tried your example on cfront 2.0, with similar results. Replacing
  Tracer("foo");
by Tracer a ("foo");  // the name a is not used anywhere else
also seemed to give the "expected" behaviour.

I've seen a claim that this technique may also be used to implement a lock:
  { Lock (lock_id);	// Lock is a constructor: makes an object which
                        // locks the identified lock and remembers its identity
     // critical region
  }  // destructor sees that unlock happens at close of scope

If the behaviour you report here is typical, this is clearly not a safe
alternative to

  lock(lock_id);
     // critical region
  unlock(lock_id);

Any clarification of this issue would be useful.

(One advantage of introducing a name for the Tracer or Lock object is you
no longer get "warning: result of constructor call expression not used"
messages; a disadvantage is it obscures the fact that the constructor is
used purely for its side-effect.)
-- 
Philip Machanick
philip@pescadero.stanford.edu