C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about 3.6.1/1
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Thu Nov 24, 2005 5:16 pm    Post subject: Question about 3.6.1/1 Reply with quote



Hello!

I have just stumpled into something that confuses me. In 3.6.1/1 there is
the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors for
objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction of any object
with static storage duration, while construction only contains those at
namespace scope? What about the global ones?

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
peter steiner
Guest





PostPosted: Fri Nov 25, 2005 6:17 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote



Matthias Hofmann wrote:
Quote:
Hello!

I have just stumpled into something that confuses me. In 3.6.1/1 there is
the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors for
objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction of any object
with static storage duration, while construction only contains those at
namespace scope? What about the global ones?

global variables are part of the global namespace and thus in namespace
scope (3.3.5.3).

the point are probably local objects with static storage duration
(6.7), their initialization is deferred until before its block is first
entered. that means they are not actually constructed during start-up.

in contrast both namespace scope and local objects with static storage
are destructed at program termination.

this is a bit off topic, but does anyone perhaps know examples of
freestanding implementations?

-- peter


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maciej Sobczak
Guest





PostPosted: Sat Nov 26, 2005 2:33 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote



Matthias Hofmann wrote:

Quote:
I have just stumpled into something that confuses me. In 3.6.1/1 there is
the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors for
objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction of any object
with static storage duration, while construction only contains those at
namespace scope? What about the global ones?

3.3.5/3:

"A name declared outside all named or unnamed namespaces (7.3), blocks,
function declarations, function definitions and classes has global
namespace scope (also called global scope). The potential scope of such
a name begins at its point of declaration (3.3.1) and ends at the end of
the translation unit that is its declarative region. Names declared in
the global namespace scope are said to be global."

This, together with your part above, means that during startup all
objects are constructed that are declared in some namespace (including
global). The termination is different so that it also destroys static
objects defined in those blocks that were executed (like static
variables in function bodies).

The interesting thing is that these two paragraphs do not seem to cover
static data members of any classes (they have class scope, not namespace
scope), although they are also initialized during startup (defect?).
The termination part covers their destruction, because static data
members have static storage duration.


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Sat Nov 26, 2005 2:36 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

Matthias Hofmann wrote:

Quote:
I have just stumpled into something that confuses me. In
3.6.1/1 there is the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors for
objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction
of any object with static storage duration, while construction
only contains those at namespace scope? What about the global
ones?

Global objects are at namespace scope. The difference in
terminology is to account for local static objects. Such
objects are constructed the first time the flow path passes
through their defition, and not at start-up. They are
destructed during termination, however.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bob Hairgrove
Guest





PostPosted: Sat Nov 26, 2005 2:40 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

On 24 Nov 2005 12:16:37 -0500, "Matthias Hofmann"
<hofmann (AT) anvil-soft (DOT) com> wrote:

Quote:
Hello!

I have just stumpled into something that confuses me. In 3.6.1/1 there is
the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors for
objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction of any object
with static storage duration, while construction only contains those at
namespace scope? What about the global ones?

3.3.5 defines "namespace scope". I, too, find it hard to keep up with
all the subtleties of the standard legalese.

But as I understand it, 3.6.1 means that for each translation unit,
the static objects in the namespace visible to that unit are
constructed at start-up. However, construction of static objects
inside a function body are only constructed the first time the
function is called...at least the standard allows an implementation to
do this (see 6.7, paragraph 4). At termination, ALL static objects are
destroyed, regardless of whether they were initialized at start-up or
later upon their first use.

Global static objects are constructed before main() is called (or
whatever your entry point is, WinMain(), etc.)...see 3.6.2 for
additional details.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Sebastian Redl
Guest





PostPosted: Sat Nov 26, 2005 2:41 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

Matthias Hofmann wrote:

Quote:
"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors
for objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction of any object
with static storage duration, while construction only contains those at
namespace scope? What about the global ones?

I believe namespace scope includes the global namespace. This distinction is
important for statics inside functions, which have static storage duration
but are constructed when the function is first called, and statics in
classes, which have static storage duration but aare constructed, I
believe, at an implementation-defined time.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tom Widmer
Guest





PostPosted: Sat Nov 26, 2005 2:46 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote


Matthias Hofmann wrote:
Quote:
Hello!

I have just stumpled into something that confuses me. In 3.6.1/1 there is
the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors for
objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction of any object
with static storage duration, while construction only contains those at
namespace scope? What about the global ones?

There are two types of object with static storage duration - namespace
scope objects (which is a superset of global scope objects) and block
scope objects that have been declared "static" (e.g. static local
variables). During startup you need to create only the former (since
static locals are created on demand), but during termination you need
to destroy any created static locals as well as the namespace scope
objects.

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Sat Nov 26, 2005 2:49 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

* peter steiner:
Quote:

this is a bit off topic, but does anyone perhaps know examples of
freestanding implementations?

If the moderators allow: yes, Microsoft's Visual C++, as commonly used
for creating Windows GUI programs. ;-)

By default it doesn't support:

* Standard 'main'.
* Exception handling.
* RTTI.
* Scoping rules for 'for'-loops.
* 'wchar_t' as built-in type.

However, by adding a bunch of compiler switches, including specifying
the internal name of the runtime library's entry point, it can be
coerced into behaving like a hosted implementation, that is, one
supporting standard 'main' and the other things mentioned above.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Victor Bazarov
Guest





PostPosted: Sat Nov 26, 2005 2:51 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

Matthias Hofmann wrote:
Quote:
I have just stumpled into something that confuses me. In 3.6.1/1
there is the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of
constructors for objects of namespace scope with static storage
duration; termination contains the execution of destructors for
objects with static storage duration.]"

Now I wonder: Why does terminination contain the destruction of any
object with static storage duration, while construction only contains
those at namespace scope? What about the global ones?

"Global ones" are in the _global_namespace_ scope (the '::' scope).

The termination contains all statics because some statics are not
necessarily initialised until the scope in which they are defined is
actually reached (function-bound statics), and they still have to be
destroyed.

V



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Martin Eisenberg
Guest





PostPosted: Sat Nov 26, 2005 2:52 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

Matthias Hofmann wrote:

Quote:
I have just stumpled into something that confuses me. In 3.6.1/1
there is the following statement:

"[Note: in a freestanding environment, start-up and termination
is implementation-defined; start-up contains the execution of
constructors for objects of namespace scope with static storage
duration; termination contains the execution of destructors for
objects with static storage duration.]"

Now I wonder: Why does terminination contain the destruction of
any object with static storage duration, while construction only
contains those at namespace scope? What about the global ones?

Globals are at namespace scope too, see 3.3.5/3. The difference in
formulation you quote is about class- and function-scope statics.


Martin

--
Quidquid latine scriptum sit, altum viditur.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
albert.jin@gmail.com
Guest





PostPosted: Sat Nov 26, 2005 3:01 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

Static variables inside functions should also be destructed at
termination
if they have been instantiated. I think global ones are just at global
namespace scope. That's obvious and should not be confusing.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Simon Bone
Guest





PostPosted: Sat Nov 26, 2005 3:06 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

On Thu, 24 Nov 2005 12:16:37 -0500, Matthias Hofmann wrote:

Quote:
Hello!

I have just stumpled into something that confuses me. In 3.6.1/1 there is
the following statement:

"[Note: in a freestanding environment, start-up and termination is
implementation-defined; start-up contains the execution of constructors for
objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage
duration.]"

Now I wonder: Why does terminination contain the destruction of any object
with static storage duration, while construction only contains those at
namespace scope? What about the global ones?


I think the term "namespace scope" has mislead you. It is defined in 3.3.5
and 3.3.5/3 begins:

"The outermost declarative region of a translation unit is also a
namespace, called the global namespace".

In other words, global objects have their constructors called during
start-up too. And remember, all static data members of classes must be
defined at namespace scope too, so they are also subject to the same rule.

The wording for destructors is wider because it must include function
local static objects.

HTH

Simon Bone

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Sun Nov 27, 2005 4:01 am    Post subject: Re: Question about 3.6.1/1 Reply with quote

Bob Hairgrove wrote:
Quote:
On 24 Nov 2005 12:16:37 -0500, "Matthias Hofmann"
[email]hofmann (AT) anvil-soft (DOT) com[/email]> wrote:

I have just stumpled into something that confuses me. In
3.6.1/1 there is the following statement:

"[Note: in a freestanding environment, start-up and
termination is implementation-defined; start-up contains the
execution of constructors for objects of namespace scope with
static storage duration; termination contains the execution of
destructors for objects with static storage duration.]"

Now I wonder: Why does terminination contain the destruction
of any object with static storage duration, while construction
only contains those at namespace scope? What about the global
ones?

3.3.5 defines "namespace scope". I, too, find it hard to keep
up with all the subtleties of the standard legalese.

In this case (exceptionally:-), it's not that difficult. The
standard defines a global namespace, which corresponds roughly
to what the C standard called file scope. It's the "namespace"
you get when you specify :: (without any preceding name). (I
wouldn't put my hand in the fire about it, but I think
everything works more or less as if the compiler implicitly
enclosed the entire source in a namespace whose name was "" --
the empty string.)

Quote:
But as I understand it, 3.6.1 means that for each translation
unit, the static objects in the namespace visible to that unit
are constructed at start-up. However, construction of static
objects inside a function body are only constructed the first
time the function is called...at least the standard allows an
implementation to do this (see 6.7, paragraph 4).

The standard forbids that a local static object be constructed
until the execution path passes over its definition.

Also, the standard is somewhat vague as to when the
initialization of statics at namespace scope actually occurs; it
provides for two scenarios: any time before main, or, if after
main, before the first use of any object or function in the
translation unit.

Quote:
At termination, ALL static objects are destroyed, regardless
of whether they were initialized at start-up or later upon
their first use.

All static objects which were actually constructed. If the
control flow never causes a local static to be constructed, that
static should not be destructed.

Also, destruction must occur in the reverse order of
construction.

Quote:
Global static objects are constructed before main() is called
(or whatever your entry point is, WinMain(), etc.)...see 3.6.2
for additional details.

That's true in every implementation I've ever heard of, but it
isn't guaranteed by the standard.

Be careful, too, with objects in dynamically loaded objects.
Another issue the standard ignores, but it seems safe to say
that they won't be constructed before the object is loeded.
Even if this is after main has been entered.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Simon Bone
Guest





PostPosted: Sun Nov 27, 2005 4:05 am    Post subject: Re: Question about 3.6.1/1 Reply with quote

On Sat, 26 Nov 2005 09:33:33 -0500, Maciej Sobczak wrote:

Quote:
3.3.5/3:

"A name declared outside all named or unnamed namespaces (7.3), blocks,
function declarations, function definitions and classes has global
namespace scope (also called global scope). The potential scope of such
a name begins at its point of declaration (3.3.1) and ends at the end of
the translation unit that is its declarative region. Names declared in
the global namespace scope are said to be global."

This, together with your part above, means that during startup all
objects are constructed that are declared in some namespace (including
global). The termination is different so that it also destroys static
objects defined in those blocks that were executed (like static
variables in function bodies).

The interesting thing is that these two paragraphs do not seem to cover
static data members of any classes (they have class scope, not namespace
scope), although they are also initialized during startup (defect?).
The termination part covers their destruction, because static data
members have static storage duration.

I think that is the reason why class scope static objects must have an out
of class (i.e. namespace scope) definition. Even integers initialised
within the class must have an extra definition somewhere.

Simon Bone


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Bob Hairgrove
Guest





PostPosted: Sun Nov 27, 2005 5:54 pm    Post subject: Re: Question about 3.6.1/1 Reply with quote

On 26 Nov 2005 23:01:09 -0500, James Kanze <kanze (AT) none (DOT) news.free.fr>
wrote:

Quote:
Bob Hairgrove wrote:
3.3.5 defines "namespace scope". I, too, find it hard to keep
up with all the subtleties of the standard legalese.

In this case (exceptionally:-), it's not that difficult. The
standard defines a global namespace, which corresponds roughly
to what the C standard called file scope. It's the "namespace"
you get when you specify :: (without any preceding name). (I
wouldn't put my hand in the fire about it, but I think
everything works more or less as if the compiler implicitly
enclosed the entire source in a namespace whose name was "" --
the empty string.)

The OP stumbled over the fact that both a regular namespace (i.e.
declaration of scope beginning with the word "namespace") and a class
or struct declaration scope are both considered namespace scopes, in
addition to the global namespace. I still find this confusing at times
when the standard refers to namespaces, although I do understand the
basic implications.

Quote:
But as I understand it, 3.6.1 means that for each translation
unit, the static objects in the namespace visible to that unit
are constructed at start-up. However, construction of static
objects inside a function body are only constructed the first
time the function is called...at least the standard allows an
implementation to do this (see 6.7, paragraph 4).

The standard forbids that a local static object be constructed
until the execution path passes over its definition.

But 6.7 paragraph 4 seems to read otherwise:

"[...] A local object of POD type (3.9) with static storage duration
initialized with constant-expressions is initialized before its block
is first entered. An implementation is permitted to perform early
initialization of other local objects with static storage duration
under the same conditions that an implementation is permitted to
statically initialize an object with static storage duration in
namespace scope (3.6.2). Otherwise such an object is initialized the
first time control passes through its declaration [...]"

I also find it interesting that in the last sentence, they use
"declaration", and you say "definition" ... I think "definition" is
probably better here.

Quote:
Also, the standard is somewhat vague as to when the
initialization of statics at namespace scope actually occurs; it
provides for two scenarios: any time before main, or, if after
main, before the first use of any object or function in the
translation unit.

Doesn't this depend on static vs. dynamic initialization? That's what
3.6.2 paragraph 3 seems to be saying.

Quote:
At termination, ALL static objects are destroyed, regardless
of whether they were initialized at start-up or later upon
their first use.

All static objects which were actually constructed. If the
control flow never causes a local static to be constructed, that
static should not be destructed.

<nit-pick-mode>
If some static variable was never constructed, then there is no object
to destroy. I took that as a given, but you are correct, of course.
</nit-pick-mode>

Quote:
Also, destruction must occur in the reverse order of
construction.

Global static objects are constructed before main() is called
(or whatever your entry point is, WinMain(), etc.)...see 3.6.2
for additional details.

That's true in every implementation I've ever heard of, but it
isn't guaranteed by the standard.

I believe it is guaranteed for static initialization of global static
objects (i.e. zero-initialized or initialized with a constant
expression). You're right if the static object is dynamically
initialized, however.

Quote:
Be careful, too, with objects in dynamically loaded objects.
Another issue the standard ignores, but it seems safe to say
that they won't be constructed before the object is loeded.
Even if this is after main has been entered.

I wouldn't say that the standard ignores this. After the example given
by 3.6.2 paragraph 3, it says: "It is implementation-defined whether
either a or b is initialized before main is entered or whether the
initializations are delayed until a is first used in main" (a and b
are global variables declared "extern" with class type).

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.