Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Invoking instance before main and memory allocation for static member

Reply
Thread Tools

Invoking instance before main and memory allocation for static member

 
 
Alex Vinokur
Guest
Posts: n/a
 
      07-24-2003
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
(E-Mail Removed)
http://sourceforge.net/users/alexvn
http://www.simtel.net/search.php?act...e=Alex+Vinokur
==========================================



 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      07-24-2003
"Alex Vinokur" <(E-Mail Removed)> wrote in message
news:bfnqvi$ggp8u$(E-Mail Removed)-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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Re: Invoking fuction before main Chris Torek C Programming 5 07-23-2003 01:08 AM
Re: Invoking fuction before main Dan Pop C Programming 8 07-21-2003 03:28 PM
Invoking a function before execution of main() Manuel C++ 6 07-19-2003 05:25 PM



Advertisments