Invoking instance before main and memory allocation for static member

A

Alex Vinokur

Hi,

I have a problem with invoking an instance before main() when using memory allocation for static member.

Is the program below valid?

===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================

// ====== C++ code : File t.cpp : BEGIN ======
#include <map>
#include <iostream>
using namespace std;


class Foo1 {};

template <typename T>
class Foo2
{
public :
static map<char, Foo1*> map1_s;
};

class Init
{
public :
Init();
};


// -------------------------
// Is such memory allocation legal ? :
template <typename T>
map<char, Foo1*> Foo2<T>::map1_s;
// -------------------------

// -------------------------
// Must we allocate memory as folowing ? :
// map<char, Foo1*> Foo2<int>::map1_s;
// map<char, Foo1*> Foo2<string>::map1_s;
// .........................
// -------------------------


Init::Init ()
{
cout << "Init ---> Start" << endl;

Foo2<int>::map1_s['A'] = new Foo1 ();

cout << "Init ---> Finish" << endl;
}


// ----------------------------
Init init;

int main(int argc, char *argv[])
{
cout << "===> Main" << endl;
return 0;
}
// ====== C++ code : File t.cpp : END ========


// ====== Compilation & Run : BEGIN ========

$ g++ t.cpp

$ a
Init ---> Start
Segmentation fault (core dumped)

// ====== Compilation & Run : END ==========



--
==========================================
Alex Vinokur
mailto:[email protected]
http://sourceforge.net/users/alexvn
http://www.simtel.net/search.php?action=search&authorName=Alex+Vinokur
==========================================
 
J

Jakob Bieling

Alex Vinokur said:
Hi,

I have a problem with invoking an instance before main() when using memory allocation for static member.

Is the program below valid?

===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================

// ====== C++ code : File t.cpp : BEGIN ======
#include <map>
#include <iostream>
using namespace std;


class Foo1 {};

template <typename T>
class Foo2
{
public :
static map<char, Foo1*> map1_s;
};

class Init
{
public :
Init();
};


// -------------------------
// Is such memory allocation legal ? :
template <typename T>
map<char, Foo1*> Foo2<T>::map1_s;
// -------------------------

Yes, it is legal.
// -------------------------
// Must we allocate memory as folowing ? :
// map<char, Foo1*> Foo2<int>::map1_s;
// map<char, Foo1*> Foo2<string>::map1_s;
// .........................
// -------------------------

I did not know this would even work as well!
Init::Init ()
{
cout << "Init ---> Start" << endl;

Foo2<int>::map1_s['A'] = new Foo1 ();

cout << "Init ---> Finish" << endl;
}


// ----------------------------
Init init;

I think you have a problem here. Afaik, it is not specified in which
order those global objects are constructed. So it might well happen that the
Initobject is constructed before the map inside the Foo object is
constructed. In that case the Init object would access a map that has not
yet been constructed, giving you the seg fault below.
int main(int argc, char *argv[])
{
cout << "===> Main" << endl;
return 0;
}
// ====== C++ code : File t.cpp : END ========


// ====== Compilation & Run : BEGIN ========

$ g++ t.cpp

$ a
Init ---> Start
Segmentation fault (core dumped)

// ====== Compilation & Run : END ==========


So even though I would call this program valid, you (probably?) have a
problem with the order of construction. Try removing the "new Foo" line
inside the Init c'tor .. it should work then.

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top