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 

What's wrong with this code?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
loose AT astron DOT nl
Guest





PostPosted: Wed May 09, 2007 3:11 pm    Post subject: What's wrong with this code? Reply with quote



Hi,

Could anyone tell me what's wrong with the code below? It segfaults
when the first element is being inserted in the map.

<code>
#include <map>
#include <string>
#include <iostream>

using namespace std;

class Init
{
public:
Init();
private:
static map<int, string> map_;
static Init init_;
};

Init::Init()
{
map_[1] = "One"; // <--- Segfault here
}

Init Init::init_;
map<int, string> Init::map_;

int main()
{
return 0;
}
</code>

Kind regards,

Marcel Loose.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Vladimir Marko
Guest





PostPosted: Wed May 09, 2007 7:39 pm    Post subject: Re: What's wrong with this code? Reply with quote



On 9 May, 16:11, loose AT astron DOT nl <l...@astron.nl> wrote:
Quote:
Hi,

Could anyone tell me what's wrong with the code below? It segfaults
when the first element is being inserted in the map.

code
#include <map
#include <string
#include <iostream

using namespace std;

class Init
{
public:
Init();
private:
static map<int, string> map_;
static Init init_;

};

Init::Init()
{
map_[1] = "One"; // <--- Segfault here

}

Init Init::init_;
map<int, string> Init::map_;

int main()
{
return 0;}

/code

The offending line is trying to use Init::map_ before it was
constructed. Init::init_ is guaranteed to be constructed
before Init::map_ because it's defined earlier in the same
translation unit. If you change the definition order, it
should work. It's a little fragile, however, and there are
better ways of constructing singletons. Try google :)

Regards,
Vladimir Marko


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jens Kilian
Guest





PostPosted: Wed May 09, 2007 7:41 pm    Post subject: Re: What's wrong with this code? Reply with quote



loose AT astron DOT nl <loose (AT) astron (DOT) nl> writes:
Quote:
Could anyone tell me what's wrong with the code below?

The constructor for init_ is called before the one for map_.

Swap the following lines:

Quote:
Init Init::init_;
map<int, string> Init::map_;

--
mailto:jjk (AT) acm (DOT) org As the air to a bird, or the sea to a fish,
http://www.bawue.de/~jjk/ so is contempt to the contemptible. [Blake]
http://del.icio.us/jjk

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






PostPosted: Wed May 09, 2007 7:44 pm    Post subject: Re: What's wrong with this code? Reply with quote

Hi Marcel,

Your problem lies in the order in which the static objects are
created. Since map_ is static and so is init_, your segfault probably
comes from the fact that init_ is created first, and so accesses map_,
which is scheduled to be created once Init() is finished.

To the others: there is no rule for the initialization of static
objects between modules, but how about statics in a same module? From
the example, I would guess that they are created in the order in which
they are defined, not declared. But is that specified?

Cheers,

Carl


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
red floyd
Guest





PostPosted: Wed May 09, 2007 8:15 pm    Post subject: Re: What's wrong with this code? Reply with quote

loose AT astron DOT nl wrote:
Quote:
Hi,

Could anyone tell me what's wrong with the code below? It segfaults
when the first element is being inserted in the map.

Possibly the static initialization fiasco? Try swapping the lines
marked A and B below.
Quote:

code
#include <map
#include <string
#include <iostream

using namespace std;

class Init
{
public:
Init();
private:
static map<int, string> map_;
static Init init_;
};

Init::Init()
{
map_[1] = "One"; // <--- Segfault here
}

LINE A
Init Init::init_;
LINE B
map<int, string> Init::map_;


int main()
{
return 0;
}
/code

Kind regards,

Marcel Loose.



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





PostPosted: Wed May 09, 2007 8:15 pm    Post subject: Re: What's wrong with this code? Reply with quote

On 9 Mag, 17:11, loose AT astron DOT nl <l...@astron.nl> wrote:
Quote:
map_[1] = "One"; // <--- Segfault here
map_ is not an object during this segfault



Quote:
Init Init::init_; // Segfault here

map<int, string> Init::map_; // Life of map begin here

Change to:
map<int, string> Init::map_; // Life of map begin here
Init Init::init_; // Hopefully ok


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Martin Vuille
Guest





PostPosted: Wed May 09, 2007 8:15 pm    Post subject: Re: What's wrong with this code? Reply with quote

loose AT astron DOT nl <loose (AT) astron (DOT) nl> wrote in
news:1178705110.137632.63970 (AT) w5g2000hsg (DOT) googlegroups.com:

Quote:

class Init
{
public:
Init();
private:
static map<int, string> map_;
static Init init_;
};

Init::Init()
{
map_[1] = "One"; // <--- Segfault here
}

Init Init::init_;
map<int, string> Init::map_;

These objects are initialized in order of declaration.
Since the Init c'tor uses the map, the map must be
declared first so that it will be initialized before
first use.

MV

--
I do not want replies; please follow-up to the group.

[ 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: Wed May 09, 2007 8:16 pm    Post subject: Re: What's wrong with this code? Reply with quote

{ Edits: double-spacing removed. -mod }

On Wed, 9 May 2007 09:11:02 CST, loose AT astron DOT nl
<loose (AT) astron (DOT) nl>
wrote:

Quote:
Hi,

Could anyone tell me what's wrong with the code below? It segfaults
when the first element is being inserted in the map.

[snip]
Quote:
Init Init::init_;
map<int, string> Init::map_;
[snip]


Since these are in the same translation unit, they are initialized in
the order in which they are defined (see section 3.6.2 of the C++
standard). Switch the order of these lines and it works OK.

--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com


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





PostPosted: Wed May 09, 2007 8:17 pm    Post subject: Re: What's wrong with this code? Reply with quote

On 9 Mai, 17:11, loose AT astron DOT nl <l...@astron.nl> wrote:
Quote:
Hi,

Could anyone tell me what's wrong with the code below?


Hi Marcel,

the order of initialisation of static data members depends only from
the order of their definitions.
You have to switch the definitions of Init::init_ and Init::map_:

map<int, string> Init::map_;
Init Init::init_;


K.



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





PostPosted: Wed May 09, 2007 8:17 pm    Post subject: Re: What's wrong with this code? Reply with quote

On May 9, 11:11 am, loose AT astron DOT nl <l...@astron.nl> wrote:
Quote:
Hi,

Could anyone tell me what's wrong with the code below? It segfaults
when the first element is being inserted in the map.

There is no map - yet.

Quote:

code
#include <map
#include <string
#include <iostream

using namespace std;

class Init
{
public:
Init();
private:
static map<int, string> map_;
static Init init_;

};

Init::Init()
{
map_[1] = "One"; // <--- Segfault here

}

Init Init::init_;

The above invokes Init's ctor before the static map comes to life.
try switching these two statements.
After all, isn't it natural that the members of a type be constructed
first?

Quote:
map<int, string> Init::map_;

int main()
{
return 0;}

/code

Kind regards,

Marcel Loose.





--
[ 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: Wed May 09, 2007 11:39 pm    Post subject: Re: What's wrong with this code? Reply with quote

{ Double-spacing corrected. -mod }

On Wed, 9 May 2007 13:44:01 CST, carl.seleborg (AT) gmail (DOT) com wrote:

Quote:
...is that specified?
Yes: see section 3.6.2 of the standard.


--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com

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





PostPosted: Thu May 10, 2007 1:07 am    Post subject: Re: What's wrong with this code? Reply with quote

red floyd wrote:
Quote:
loose AT astron DOT nl wrote:
Hi,

Could anyone tell me what's wrong with the code below? It segfaults
when the first element is being inserted in the map.

Possibly the static initialization fiasco? Try swapping the lines
marked A and B below.

It just occurs to me that the term "static initialization fiasco"
is misleading: because it doesn't have anything to do with
"static initialization", but only with "dynamic initialization".

It's true that it happens with static member objects, but non-member
objects suffer from the same phenomenon; the word "static" here probably
came from "static storage duration".

3.6.2/1: "Objects with *static storage duration* defined in namespace
scope in the same translation unit and *dynamically initialized* shall
be initialized in the order in which their definition appears in the
translation unit." (emphasis mine)

--
Seungbeom Kim

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





PostPosted: Thu May 10, 2007 1:12 am    Post subject: Re: What's wrong with this code? Reply with quote

Martin Vuille wrote:
Quote:
loose AT astron DOT nl <loose (AT) astron (DOT) nl> wrote in
news:1178705110.137632.63970 (AT) w5g2000hsg (DOT) googlegroups.com:

Init Init::init_;
map<int, string> Init::map_;

These objects are initialized in order of declaration.

Not in order of declaration, but in order of definition.

Quote:
Since the Init c'tor uses the map, the map must be
declared first so that it will be initialized before
first use.

The map must be "defined" first.

--
Seungbeom Kim

[ 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
Page 1 of 1

 
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.