🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Wesley E. Munsil Mon, 25 Sep 1995 21:19
Questions concerning initialization of nonlocals
#2039
Author: Wesley E. Munsil
Date: Mon, 25 Sep 1995 21:19
31 lines
1780 bytes
Please consider the following program schema and questions.

// Declare class A with non-trivial constructor
#include "a.h"

static A a;

int main()
{
   // Nontrivial use of a
   return 0;
}

(1) Will the standard require that a's constructor be called?

(2) If the answer to (1) is "yes", will the standard impose any constraints
    as to the time at or by which the call must occur?

(3) If the answer to (1) is "yes" and the answer to (2) is "no", does not
    this imply that a compiler is free to time the call to occur whenever
    it likes, say, immediately prior to termination of the program?

(4) If the answer to (1) is "yes" and the answer to (2) is "yes", what are
    those constraints?  Please answer in the most general form.

Thank you.


[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Questions concerning initialization of nonlocals
#2042
Author: Ajay Kamdar
Date: Tue, 26 Sep 1995 16:49
54 lines
2300 bytes
In article <DFHC1v.Kq5@peacock.tcinc.com>,
Wesley E. Munsil <wmunsil@tcinc.com> wrote:
>Please consider the following program schema and questions.
>
>// Declare class A with non-trivial constructor
>#include "a.h"
>
>static A a;
>
>int main()
>{
>   // Nontrivial use of a
>   return 0;
>}
>
>(1) Will the standard require that a's constructor be called?

Yes.

>(2) If the answer to (1) is "yes", will the standard impose any constraints
>    as to the time at or by which the call must occur?

Yes.

>(4) If the answer to (1) is "yes" and the answer to (2) is "yes", what are
>    those constraints?  Please answer in the most general form.

In the example above, the constructor for "a" will be called before the
start of main.

If the static object above were not in the same translation unit as main(),
then a copule of other possibilities exist. In such a situation,
theoritically, the ctor could be executed some time before the start of
main(), OR it could be deferred to any point in time before the first use
of an object or function in that translation unit.

Note that the DWP explicitly states that the order in which non-local
objects in different translation units will be initialized is not specified.
In fact, program execution errors due to incorrect order of non-local
static object initialization (i.e., different from what the programmer
thought it would be) are very common.

DWP 3.6.2 "Initialization of non-local objects" has more on this topic. 


-- 
Ajay Kamdar        |    Email: ajay@lehman.com    |    Standard Disclaimer 
Lehman Brothers    |    Phone: (201) 524-5048     |

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Questions concerning initialization of nonlocals
#2044
Author: Wesley E. Munsil
Date: Tue, 26 Sep 1995 19:51
26 lines
1532 bytes
ajay@lehman.com (Ajay Kamdar) wrote:

..

>If the static object above were not in the same translation unit as main(),
>then a copule of other possibilities exist. In such a situation,
>theoritically, the ctor could be executed some time before the start of
>main(), OR it could be deferred to any point in time before the first use
>of an object or function in that translation unit.

..

On the contrary, a noted compiler vendor, active on the committee, has told me
that this was rendered inoperative at the last meeting -- in fact, the
constructor call may occur AFTER the use of some object or function in the
translation unit, since enforcing the policy stated in the DWP is "impossible".

Thus, my questions.  Will the standard impose any constraints as to the time at
or by which the call must occur?


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
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