473,401 Members | 2,068 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,401 software developers and data experts.

non-static global variables and the extern qualifier

A
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable

class Foo{
...
};
The scope of i is throughout the entire program since it is global and
non-static. Lets say i want to assign a new value to i in another file
called Bar.h, how is it done?

//Header File - Bar.h

i = 1 //like this? or must i use the extern qualifier here? confusion here
is that it i is global then we u need extern to make it external to another
file when it should be already available?

class Bar{
...
};
Regards,
A
Jul 22 '05 #1
5 3186
A wrote:
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable

class Foo{
...
};
The scope of i is throughout the entire program since it is global and
non-static.
Right, but you defined i in a header, and if you #include that header in
more than one translation unit, you'll get a linker error for multiple
definitions. Rather define it in the .cpp file, and just declare it in
the header.
Lets say i want to assign a new value to i in another file
called Bar.h, how is it done?

//Header File - Bar.h

i = 1 //like this?
Well, not in the global scope, but within a function, yes.
or must i use the extern qualifier here?
No, but you must have declared it extern.
confusion
here is that it i is global then we u need extern to make it external
to another file when it should be already available?


You should do it something like this:

Foo.cpp:

int i = 0;
//...

Foo.h:

extern int i;

Bar.cpp:

#include "Foo.h"

void somefunc()
{
i = 1;
}

i is defined in Foo.cpp and is global, but other .cpp files like Bar.cpp
don't know anything about it. Just like you have to make a type or a
function from another tranlation unit known, you have to make a
variable known. The "extern int i" tells the compiler that there is a
global variable i of type int that defined elsewhere.

Jul 22 '05 #2
A

A wrote:
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable

class Foo{
...
};
The scope of i is throughout the entire program since it is global and
non-static.


Right, but you defined i in a header, and if you #include that header in
more than one translation unit, you'll get a linker error for multiple
definitions. Rather define it in the .cpp file, and just declare it in
the header.


Firstly, i agree with the error.

#include would obviously bring i into scope wherever you included it, and
thus defeats the purpose of the external qualifier? I gather from what Ive
read that by using the external qualifer you can open the scope to multiple
files - it does not make mention of also using #include.

I was thinking more on the lines:

Foo.h

int i = 0; // non-static global variable

class Foo{
...
};

Bar.h

extern int i; //refers to the i declared in Foo.h; no need to use #include

class Bar{
...
};
Regards,
A
Jul 22 '05 #3
A wrote:

A wrote:
> Hi,
>
> Consider this code:
>
> //Header File - Foo.h
>
> int i = 0; // non-static global variable
>
> class Foo{
> ...
> };
>
>
> The scope of i is throughout the entire program since it is global
> and non-static.
Right, but you defined i in a header, and if you #include that header
in more than one translation unit, you'll get a linker error for
multiple definitions. Rather define it in the .cpp file, and just
declare it in the header.


Firstly, i agree with the error.

#include would obviously bring i into scope wherever you included it,
and thus defeats the purpose of the external qualifier?


Why?
If you write:

int i = 0;

it lets the compiler know there is an int named i _and_ that it should
allocate space for that int.

extern int i;

also lets the compiler know there is an int named i, but it does _not_
allocate storage for it. So the extern just means "there is a variable,
but it alredy exists somewhere else". The linker will resolve it then.
I gather from what Ive read that by using the external qualifer you
can open the scope to multiple files - it does not make mention of
also using #include.
I was thinking more on the lines:

Foo.h

int i = 0; // non-static global variable

class Foo{
...
};


Well, this would only be ok as long as you #include "Foo.h" only _once_
in your whole program. But that would defeat the purpose of headers.
Bar.h

extern int i; //refers to the i declared in Foo.h; no need to use
#include
Right. But headers are just there to make things from one translation
unit known to another one. You put it all in a header so that you don't
need to repeat everything wherever it is needed. And just as you would
put a function declaration in the header and define it in the .cpp
file, you should only declare your global variables (and that's what
the extern actually means) in the header and put the definition in
the .cpp file.
class Bar{
...
};


Jul 22 '05 #4
On Wed, 26 Nov 2003 21:32:53 +1030, "A" <A@iprimus.com.au> wrote in
comp.lang.c++:
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable
Actually, i is defined without the static keyword, but it is indeed a
static object, meaning that is has static storage duration.
class Foo{
...
};
The scope of i is throughout the entire program since it is global and
No, you are confusing scope with linkage. The definition of i has
namespace scope in C++, and since it is at top level in the global
namespace, it is basically the same as what is called "file scope" in
C, even though that terminology is no longer used in C++.

The definition is in scope from the point where it appears in the
translation unit until the end of the translation unit, although it
may be hidden by another usage of the same name in an inner block.
non-static. Lets say i want to assign a new value to i in another file
called Bar.h, how is it done?
You can only assign a value to an object in code. You can only
initialize an object in its definition. So if i is defined in a
source file, you can't initialize it anywhere else, and you must
assign to it from inside a function, not at file level.
//Header File - Bar.h

i = 1 //like this? or must i use the extern qualifier here? confusion here
is that it i is global then we u need extern to make it external to another
file when it should be already available?

class Bar{
...
};
Regards,
A


You have some confusion over three separate but somewhat related
concepts, scope, linkage, and storage duration. Part of the confusion
stems from the fact that C++ inherited two different meanings of the
"static" keyword from C, not to mention adding another meaning of its
own.

Any object defined outside of a function has static storage duration,
which means it exists for the life of your program. Adding the
keyword "static" or the keyword "const" to such an object does not
change the storage duration, which is still static, and does not
change the scope, which is still to the end of the translation unit,
it merely changes the linkage from internal to external.

External linkage means that code in another translation unit may refer
to the object by name, provided that that code has a suitable external
declaration in its scope.

And the definition of translation unit is basically the source file
you pass to the compiler plus everything it includes.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #5
"Jack Klein" <ja*******@spamcop.net> wrote...
[...]
Any object defined outside of a function has static storage duration,
which means it exists for the life of your program. Adding the
keyword "static" or the keyword "const" to such an object does not
change the storage duration, which is still static, and does not
change the scope, which is still to the end of the translation unit,
it merely changes the linkage from internal to external.


Vice versa, actually. "static" changes linkage from external to internal.

Victor
Jul 22 '05 #6

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
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: 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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.