Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Invoking instance before main and memory allocation for static member (http://www.velocityreviews.com/forums/t275990-invoking-instance-before-main-and-memory-allocation-for-static-member.html)

Alex Vinokur 07-24-2003 05:30 AM

Invoking instance before main and memory allocation for static member
 
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:alexvn@connect.to
http://sourceforge.net/users/alexvn
http://www.simtel.net/search.php?act...e=Alex+Vinokur
==========================================




Jakob Bieling 07-24-2003 12:46 PM

Re: Invoking instance before main and memory allocation for static member
 
"Alex Vinokur" <alexvn@bigfoot.com> wrote in message
news:bfnqvi$ggp8u$1@ID-79865.news.uni-berlin.de...
> 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
--
jb

(replace y with x if you want to reply by e-mail)




All times are GMT. The time now is 02:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.