Topic: Ordering constraints on static constructors/destructors


Author: tom@sco.COM (Tom Kelly)
Date: 22 Aug 90 20:08:05 GMT
Raw View
In section 12.6.1 of the ARM, it says:

 Constuctors for non-local static objects are called in the
 order they occur in a file; destructors are called in
 reverse order.  See also 3.4, 6.7, 9.4.

In section 3.4, it says:

 The initialization of non-local static objects (3.5) in
 a file is done before the first use of any function
 or object defined in that file. ... No further order
 is imposed on the initialization of objects from different
 files.

These rules impose a partial order on the intialization of non-local
static objects.  Typically, the implementation chooses a total
ordering that is compatible with the partial order.

Later in section 3.5 it says:

 Destructors (12.4) for initialized static objects are called
 when returning from main() and when calling exit().
 Destruction is done in reverse order of initialization.

Does the last sentence mean that the destructor order is constrained
to be the reverse of the total order actually executed for this run
of the program, or just that it must be compatible with the reversed
partial order?

For example, if I have in file1:

foo A;
foo B;

and in file2

foo C;

then construct A, construct B, construct C is a valid ordering for
initialization.  Is destruct B, destruct A, destruct C a valid
ordering for termination?

Tom Kelly  (416) 922-1937
SCO Canada, Inc. (formerly HCR) 130 Bloor St. W., Toronto, Ontario, Canada
{utzoo, utcsri, uunet}!scocan!tom




Author: ark@alice.UUCP (Andrew Koenig)
Date: 23 Aug 90 16:40:00 GMT
Raw View
In article <1990Aug22.160805.8519@sco.COM>, tom@sco.COM (Tom Kelly) writes:

>  Destructors (12.4) for initialized static objects are called
>  when returning from main() and when calling exit().
>  Destruction is done in reverse order of initialization.

> Does the last sentence mean that the destructor order is constrained
> to be the reverse of the total order actually executed for this run
> of the program, or just that it must be compatible with the reversed
> partial order?

It means that the objects must be destroyed in precisely the reverse
order of their construction.

It's not good enough to pick a random one of the inverses of the
partial ordering.
--
    --Andrew Koenig
      ark@europa.att.com