472,805 Members | 915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 3135
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.