473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Global (static) Var in a library initialization question.

Hi,

I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.

Thanks!

If you run sbin (linking with .a), the output is 0 however if
you run shbin(linking with .so) the output is one.
Tried with GCC 3.4

///////////////////////////////////////////
//lib.h
#include <iostream>
#include <string>
#include <vector>

template <class T>
class SingleTon
{
public:
static T * instance ();

protected:

SingleTon () {}
static T * instance_;
};

class A : public SingleTon<A>
{
public:
A () {}
int size () { return box_.size (); }
void push (int i) { box_.push_back (i); }
int pop ()
{
int back = box_.back ();
box_.pop_back ();
return back;
}
protected:
std::vector<int> box_;
};

template <class T>
T * SingleTon <T>::instance_ = 0;

template <class T>
T * SingleTon <T>::instance ()
{
if (!instance_) instance_ = new T;
return instance_;
}

/////////////////////////
//test.h
#include "lib.h"

class B
{
public:
B ();
};

//////////////////////////////
//test.cpp
#include "test.h"

B b;
B::B () { A::instance () -> push (5); }

/////////////////////////////
//main.cpp
#include "test.h"

int main ()
{
printf ("current size of the box is : %d\n", A::instance()->size());
}

#////////////////////////////////
#Makefile
all : sbin shbin

test.o : test.h test.cpp lib.h
g++ -g -c test.cpp -o test.o

libtest.a : test.o
ar rsuv libtest.a test.o

libtest.so : test.o
g++ -g -shared -o libtest.so test.o

sbin : libtest.a
g++ -g main.cpp -o sbin ./libtest.a

shbin : libtest.so
g++ -g main.cpp -o shbin ./libtest.so

..PHONY : clean
clean :
rm *.o *.so *.a sbin shbin

Nov 10 '05 #1
6 2545
Kyle wrote:
I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.


Look in the FAQ for "fiasco"

V
Nov 10 '05 #2
Hi Victor,

Thanks for the help. Actually I thought about the fiasco order problem
when I was working on the orignal problem which lead to this simplified
code snip, however, I don't think that this is the same case.

In my code there is no dependency amoung two object files unless you
count the one which contains no global var but only main func. Maybe I
am missing something.

Thanks!

Nov 10 '05 #3

Kyle wrote:
Hi,

I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.

//////////////////////////////
//test.cpp
#include "test.h"

B b;
B::B () { A::instance () -> push (5); }

/////////////////////////////
//main.cpp
#include "test.h"

int main ()
{
printf ("current size of the box is : %d\n", A::instance()->size());
}


Is the rule is that B b; must be initialized before any function in
test.cpp is called, or before main() is called? Note that there are no
functions in test.cpp to be called. I'm tempted to think that the
compiler/linker could throw out test.cpp completely...

Tony

Nov 10 '05 #4
On Wed, 09 Nov 2005 21:51:00 -0800, Kyle wrote:
Thanks for the help. Actually I thought about the fiasco order problem
when I was working on the orignal problem which lead to this simplified
code snip, however, I don't think that this is the same case.

In my code there is no dependency amoung two object files unless you
count the one which contains no global var but only main func. Maybe I
am missing something.


You're right, I ought to look a little more carefully. Have you tried
putting some side effect (besides calling a function in A) into the B's
constructor? Does it actually get called?

V
Nov 10 '05 #5
Kyle wrote:
Hi,

I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.
Technically, issues related to static libraries are an operating
system/tools problem and should be taken to the appropriate forum for
your tools.

Thanks!

If you run sbin (linking with .a), the output is 0 however if
you run shbin(linking with .so) the output is one.
Tried with GCC 3.4

///////////////////////////////////////////
//lib.h
#include <iostream>
#include <string>
You don't use these two headers in this file. Don't include them here.
#include <vector>

template <class T>
class SingleTon
{
public:
static T * instance ();

protected:

SingleTon () {}
static T * instance_;
};

class A : public SingleTon<A>
{
public:
A () {}
int size () { return box_.size (); }
void push (int i) { box_.push_back (i); }
int pop ()
{
int back = box_.back ();
box_.pop_back ();
return back;
}
protected:
std::vector<int> box_;
};

template <class T>
T * SingleTon <T>::instance_ = 0;

template <class T>
T * SingleTon <T>::instance ()
{
if (!instance_) instance_ = new T;
return instance_;
}

/////////////////////////
//test.h
#include "lib.h"
You don't use this header in this file, so don't include it here. It
can speed up compile times to include the minimal amount, especially on
large projects.

class B
{
public:
B ();
};

//////////////////////////////
//test.cpp
#include "test.h"
Add:
#include "lib.h"

B b;
B::B () { A::instance () -> push (5); }

/////////////////////////////
//main.cpp
#include "test.h"
Add:
#include <iostream>

int main ()
{
printf ("current size of the box is : %d\n", A::instance()->size());
Ewww. Prefer the type-safe std::cout over std::printf.
}

[snip]

First, I would suggest renaming your class from SingleTon (which most
would interpret as a solitary heavy-weight) to Singleton, the
conventional designation for the design pattern.

Second, note that inheriting Singleton<A> does not make A a singleton
because the user can still easily create an independent instance or
copy the singleton instance, meaning there can be more than one A.
E.g.,

A a1( *A::instance() );
A a2;

Check out _Modern C++ Design_ chapter 6 for more than you ever wanted
to know about singletons. The book discusses some pitfalls and tricks
to creating singletons. You can download the library code from:

http://sourceforge.net/projects/loki-lib/

Check out Singleton.h.

Cheers! --M

Nov 10 '05 #6
Tony,

You are right, the test.o is not linked in at all.....
Thanks!

-Kyle

Nov 10 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
1
by: Oystein Haare | last post by:
Note: This might be a bit off topic.. I want to create some global objects that register themselves with another "manager"-class upon creation: class SomeClass { ... }; // cpp:
4
by: Cheng Mo | last post by:
I know global varaibles should always be avoided. I asked this question just for deep insight about C++. If global variables are distributed among different source code files, what's the...
3
by: Rahul Gandhi | last post by:
Hi, Which one preferable with respect to code size of the executable Un-initialised global variables or initialised global variables regards Rahul
2
by: Vinu | last post by:
Hi, I am facing one problem related to global variables in .so file. When ever I access the variable the application is crashing I have a class called services. class services { services(){...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
10
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: ...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.