473,748 Members | 9,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable declaration: Global vs. Function

Hi,

I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).

class Book
{
public:
Book()
{ ; }
virtual ~Book() { ; }
string get_title(void) const
{ return title;}
private:
std::string title;
std::string author;
};

/* Global scope declaration */
Book Global_Book;

/* Inside function declaration */
void Any_Function(vo id)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_ti tle();
return;
}

I am throwing this code into a library, and the library
reports:
U global destructors keyed to Global_Book
U global constructors keyed to Global_Book
If I comment-out the global declaration, these messages
disappear {of course}.

So, why are there constructors & destructors for a global
instance, but not for an instance local to the function?

If I change the declaration of "any_book" in the function
to static, there are still no "global constructors" listed
for it (I would think there would be).

I am using g++ (GCC) 3.3.1 (cygming special).

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #1
2 8831
"Thomas Matthews" <Th************ *************@s bcglobal.net> wrote in
message news:41******** ******@sbcgloba l.net...
Hi,

I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).

class Book
{
public:
Book()
{ ; }
virtual ~Book() { ; }
Note that those empty statements in your
ctor and dtor are not needed.
string get_title(void) const
{ return title;}
private:
std::string title;
std::string author;
};

/* Global scope declaration */
Book Global_Book;

/* Inside function declaration */
void Any_Function(vo id)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_ti tle();
return;
}

I am throwing this code into a library,
So now we can't know the full context, which imo
is very likely relevant.

and the library
reports:
U global destructors keyed to Global_Book
U global constructors keyed to Global_Book
The 'library' reports? Or a compiler, or a linker?
I don't understand.
If I comment-out the global declaration, these messages
disappear {of course}.
I don't know what those messages about 'keyed' mean.
What does your documentation say?

So, why are there constructors & destructors for a global
instance, but not for an instance local to the function?
Must be some propery of the library, or your library manager.
A wild guess: Since I suspect you've only showed a 'skeleton'
example, I'll ask: do any of your global objects depend upon
other global objects for construction, in a particular order
(the language does not specify this order for global objects
between translation units.

If I change the declaration of "any_book" in the function
to static, there are still no "global constructors" listed
for it (I would think there would be).
'static' does not mean 'global'. Inside a function, it
indicates a storage duration, nothing to do with scope.

I am using g++ (GCC) 3.3.1 (cygming special).


I think you might have a platform-specific or tool-specific
issue, but I can't say conclusively without more context.
Have you tried any methodical, incremental build-and-testing?

-Mike
Jul 23 '05 #2
Mike Wahler wrote:
"Thomas Matthews" <Th************ *************@s bcglobal.net> wrote in
message news:41******** ******@sbcgloba l.net...
Hi,

I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).

class Book
{
public:
Book()
{ ; }
virtual ~Book() { ; }

Note that those empty statements in your
ctor and dtor are not needed.


Noted, but I do that for sytlistic purposes.
So, what's a little wasted compiler time, in
the big picture? {Rhetorical}


string get_title(void) const
{ return title;}
private:
std::string title;
std::string author;
};

/* Global scope declaration */
Book Global_Book;

/* Inside function declaration */
void Any_Function(vo id)
{
static std::string book_title;
Book any_book;
book_title = any_book.get_ti tle();
return;
}

I am throwing this code into a library,

So now we can't know the full context, which imo
is very likely relevant.


In other words, there is a third party involved:
source -> compiler -> librarian -> linker.
Unix libraries and builds have always been a
thorn for me.
and the library
reports:
U global destructors keyed to Global_Book
U global constructors keyed to Global_Book

The 'library' reports? Or a compiler, or a linker?
I don't understand.

Actually, this is a report generated from the library.
The 'U' indicates undefined symbol in the library.
In Cygwin / unix terms:
nm --demangle my_library.a

If I comment-out the global declaration, these messages
disappear {of course}.

I don't know what those messages about 'keyed' mean.
What does your documentation say?

Yep, that's what I'm having an issue with.
I was wondering if the C++ language specification had
any information about construction of global objects
and how it differs from construction of function-local
or static variables in functions.

So, why are there constructors & destructors for a global
instance, but not for an instance local to the function?

Must be some propery of the library, or your library manager.
A wild guess: Since I suspect you've only showed a 'skeleton'
example, I'll ask: do any of your global objects depend upon
other global objects for construction, in a particular order
(the language does not specify this order for global objects
between translation units.


My understanding is that constructors are called for all
objects. Just the order of construction and residence of
the object are the difference between global, function-local
and function-static variables.
If I change the declaration of "any_book" in the function
to static, there are still no "global constructors" listed
for it (I would think there would be).

'static' does not mean 'global'. Inside a function, it
indicates a storage duration, nothing to do with scope.


Understood. A variable declared as static within a function
has the same life time as a global variable. Many compilers
place these two kinds of variables in the same area. My
understanding is that the global variable is constructed
sometime before "main" and the function-static variable is
construction upon the first entry to the function (the basis
of the Singleton pattern}.

I am using g++ (GCC) 3.3.1 (cygming special).

I think you might have a platform-specific or tool-specific
issue, but I can't say conclusively without more context.
Have you tried any methodical, incremental build-and-testing?

-Mike


The issue came about because I had a static constant array
of Books. I narrowed down to just one global instance
(removing the module static declaration). This gives the
error. However, sticking the variable inside a function
does not generate the error. This is the basis for
my issue. I have instances of this variable in many
functions. Its only the global declaration that is the
problem.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #3

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

Similar topics

83
6513
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
7
1984
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename(); somename.show() Note that the class name "somename" is reused for the variable name.
134
7894
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
23
19202
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
13
1433
by: Joe Attardi | last post by:
Over the weekend I attended a session on JavaScript at the No Fluff Just Stuff conference and learned an interesting quirk that I wanted to ask a question about.. Take this code: x = 5; function foo() { alert(x);
4
2719
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any function, it is global regardless of whether it's declared with or without "var" * When it is declared inside a function, if declared with "var", it's local, if not, it's global
2
4714
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
2
3682
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
5460
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.