public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "broeni at osb-systems dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/26123] [3.4/4.0 Regression] Segmentation fault in constructor of std::ostream::sentry::sentry
Date: Tue, 07 Feb 2006 12:52:00 -0000	[thread overview]
Message-ID: <20060207125248.23188.qmail@sourceware.org> (raw)
In-Reply-To: <bug-26123-12142@http.gcc.gnu.org/bugzilla/>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2047 bytes --]



------- Comment #9 from broeni at osb-systems dot com  2006-02-07 12:52 -------
Obviously the behavior of the test program is undefined.
The following is the reply from Alberto Ganesh Barbati in c.l.c++.m

Apart from the fact that you need to include <ostream> in order to be
able to write on std::cout, yes: it's well defined. However, its
behaviour is unspecified. See below.

> In particular: Does the standard guarantee that std::cout is
> initialized before the constant `cInt'?

No, the standard does not guarantee that (although it non-normatively
encourages implementations to do it). According to §27.3/2: "The objects
[std::cin, std::cout, etc.] are constructed, and the associations are
established at some time prior to or during first time an object of
class ios_base::Init is constructed, and in any case before the body of
main begins execution."

So in order to ensure that the program behave as expected, you need to
construct a variable of type ios_base::Init before using std::cout. You
can either use a global variable like this:

int mkCint();
std::ios_base::Init gInitIostreams;
const int cInt = mkCint();

or use a local variable in function mkCint():

int mkCint()
{
     static std::ios_base::Init initIostreams;
     std::cout << "mkCint()" << std::endl;
     return 2;
}

Comment:
While the use of a global variable works fine for the reduced test #2 the
originally posted code (#1) only works with a local std::ios_base::Init in
mkCint() because of the undefined initialization order of globals.

The reply of James Kanze confirms the undefined behavior:
> Is the following program well defined?
> In particular: Does the standard guarantee that std::cout is
> initialized before the constant `cInt'?

No.

With the classical implementation of <iostream.h>, it was
guaranteed IF your code included <iostream.h> before defining
cInt.  Many (most?, all?) current implementations of <iostream>
also provide this guarantee, but the standard doesn't require
it.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26123


  parent reply	other threads:[~2006-02-07 12:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-06 12:44 [Bug libstdc++/26123] New: " broeni at osb-systems dot com
2006-02-06 12:52 ` [Bug libstdc++/26123] " pinskia at gcc dot gnu dot org
2006-02-06 12:54 ` pinskia at gcc dot gnu dot org
2006-02-06 12:56 ` [Bug libstdc++/26123] [3.4/4.0/4.1/4.2 Regression] " pinskia at gcc dot gnu dot org
2006-02-06 13:10 ` pcarlini at suse dot de
2006-02-06 13:13 ` [Bug libstdc++/26123] [3.4/4.0 " pinskia at gcc dot gnu dot org
2006-02-06 13:16 ` pcarlini at suse dot de
2006-02-06 15:07 ` rguenth at gcc dot gnu dot org
2006-02-06 19:28 ` broeni at osb-systems dot com
2006-02-07 12:52 ` broeni at osb-systems dot com [this message]
2006-02-07 13:19 ` broeni at osb-systems dot com
2006-02-07 13:59 ` pcarlini at suse dot de
2006-04-22 19:54 ` pcarlini at suse dot de

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060207125248.23188.qmail@sourceware.org \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).