• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gasto

Extern block scope variable?

19 posts in this topic

int i = 4;
int main(int argc, char* argv[])
{
    extern int i;
    cout << i;
    return 0;
}

 

In Visual Studio 2008 outputs

4

without warnings.

 

What is going on? Weren't block scope variables supposed to have no linkage?

I suspect the compiler is ignoring the fact that it is in a block altogether and simply redeclaring the global [i]i[/i]variable(applying internal linkage.)

0

Share this post


Link to post
Share on other sites

extern int i;

 

isn't a stack variable it's just telling the compiler you know that an int named i has a definition elsewhere. It doesn't even need to be defined in the same translation unit, the linker will find it if it's unique (and has external linkage).

 

You may not even get an error if i isn't really an int though.

 

EDIT: It's not good practice to extern variables at block scope, since if you do it more than once you have to change all occurrences if you change the type. You should only extern things in a single header file.

 

EDIT2: extern NEVER allocates any storage, it's just a way to tell the compiler and linker you know that something exists and has an address somewhere.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites
Also, and rather spectatularly as you tagged it as "c language"

That is **NOT** C.

Check your compiler options, double-check that you are compiling as C rather than C++.
1

Share this post


Link to post
Share on other sites

It could be C if cout is in scope and of integral type, cout << i; but it wouldn't have any useful side effects though, so would be optimised away ;)

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites
int i = 4;
int main(int argc, char* argv[])
{
    extern int i;
    cout << i;
    return 0;
}

What is going on? Weren't block scope variables supposed to have no linkage?

 

 

"int i =4" is not a block scope but whole file scope(it is even more than file scope because it is by default external)  * and "extern int i;" is a symbol declaration (not needed at all here) 

 

* a normal 'global' aso someone could name it, ane "extern int i" is 

this time not needed declaration ("part of a header" just info to the linker, nothing binary) - it is usable if you remove i to another .obj module then it is used by linker to allow you tu use that from this place 

Edited by fir
1

Share this post


Link to post
Share on other sites

That is correct but it will still work if i is not in file scope but has external linkage. Not recommended though, because of the don't repeat yourself idiom.

 

This would work too (also not good practice):

int main(int argc, char* argv[])
{
    extern int i;
    cout << i;
    return 0;
}
 
int i = 4;
Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

EDIT2: extern NEVER allocates any storage, it's just a way to tell the compiler and linker you know that something exists and has an address somewhere.

This is incorrect. Extern does allocate storage when used together with an initializer in global scope. It defines a global variable, just like any (non-static) variable defined in global scope.
 
EDIT: added a detail
0

Share this post


Link to post
Share on other sites

 

EDIT2: extern NEVER allocates any storage, it's just a way to tell the compiler and linker you know that something exists and has an address somewhere.

This is incorrect. Extern does allocate storage when used together with an initializer in global scope. It defines a global variable, just like any (non-static) variable defined in global scope.
 
EDIT: added a detail

 

Actually that throws a linker error:

Beginning.obj : error LNK2005: "int i" (?i@@3HA) already defined in otherFile.obj
0

Share this post


Link to post
Share on other sites

Basically what I've come to learn about this compiler is that it allows multiple declarations but not multiple definitions.

0

Share this post


Link to post
Share on other sites

Basically what I've come to learn about this compiler is that it allows multiple declarations but not multiple definitions.

 

ofcourse, you could populete a declaration 1000 times with no trouble

but populating a definition will raise a symbole conflict

0

Share this post


Link to post
Share on other sites

 

 

EDIT2: extern NEVER allocates any storage, it's just a way to tell the compiler and linker you know that something exists and has an address somewhere.

This is incorrect. Extern does allocate storage when used together with an initializer in global scope. It defines a global variable, just like any (non-static) variable defined in global scope.
 
EDIT: added a detail

 

Actually that throws a linker error:

Beginning.obj : error LNK2005: "int i" (?i@@3HA) already defined in otherFile.obj

Probably because you define "int i" in another file. "int i" and "extern int i=0" are the same in global scope, meaning you can only have one such definition in your whole program.

0

Share this post


Link to post
Share on other sites
extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

Edited by gasto
0

Share this post


Link to post
Share on other sites
extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

 

 

thats right this is confusing , extern means external in two ways

 

1) 'some thing is outside'

2) 'this thing is to be seen from outside' (this is default so you dont have to write it before each function)

 

same confusing is static which means 'this thing is internal only' or

this is not stack variable but 'static' '

 

this is probably done to not populate keywords in c but this was a bad choice and granted a heavy mass mind confusion ;/

1

Share this post


Link to post
Share on other sites

1) 'some thing is outside'

 

Something is assumed outside (if by that you mean program duration)also when avoiding the [i]extern[/i] keyword all together for global variable and function definitions. Yes, I tried the [i]extern[/i] keyword for global function definitions and it did not affect the compilation in any visible way.

0

Share this post


Link to post
Share on other sites

Going back to the original post, according to some documentation I am reading, at least for [b]C[/b], the extern keyword is used inside a block of code to document that any future referents (variable names) refer to the global variable as opposed to some block scope variable.

0

Share this post


Link to post
Share on other sites

 


1) 'some thing is outside'

 

Something is assumed outside (if by that you mean program duration)also when avoiding the extern keyword all together for global variable and function definitions. Yes, I tried the extern keyword for global function definitions and it did not affect the compilation in any visible way.

 

 

not program duration but is in the different seperately compiled module

 

extern has two meanings instead of one you could use word public

and instead of other declare_external_symbol like

 
void f()
{
    void g();
 
    g();
}

is short for 

 
extern void f()
{
    extern void g();
 
    g();
}

which means 

 
public void f()
{
  declare_external_symbol void g(); 
  
  g();
}

thats all about it smile.png

Edited by fir
0

Share this post


Link to post
Share on other sites
extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

 

It's not the compiler assuming anything, it's a rule in the standard. extern is not ignored. extern explicitly specifies the scope of the variable (which can be static, extern, auto, and register). That's why in global scope, you cant use extern and static within the same definition.  Most of the time it's not used, just like most of the time we don't write "signed int".

0

Share this post


Link to post
Share on other sites

 

 


1) 'some thing is outside'

 

Something is assumed outside (if by that you mean program duration)also when avoiding the extern keyword all together for global variable and function definitions. Yes, I tried the extern keyword for global function definitions and it did not affect the compilation in any visible way.

 

 

not program duration but is in the different seperately compiled module

 

extern has two meanings instead of one you could use word public

and instead of other declare_external_symbol like

 
void f()
{
    void g();
 
    g();
}

is short for 

 
extern void f()
{
    extern void g();
 
    g();
}

which means 

 
public void f()
{
  declare_external_symbol void g(); 
  
  g();
}

thats all about it smile.png

 

No, for both uses, extern means "external linkage". It's just that in one case it also means "this is a declaration", and in the other it doesn't.

0

Share this post


Link to post
Share on other sites

 

extern int i = 14;

Works, if defined in the file where it is used.

It is confusing though, an extern token should only indicate declaration not definition.

I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

 

It's not the compiler assuming anything, it's a rule in the standard. extern is not ignored. extern explicitly specifies the scope of the variable (which can be static, extern, auto, and register). That's why in global scope, you cant use extern and static within the same definition.  Most of the time it's not used, just like most of the time we don't write "signed int".

 

Actually, that is not true at all. Fir understood it right. extern simply imports a variable or function defined in another translation unit(code file), in other words it is a linkage process, the scope will stay file based, and the duration program-wise(some books call it static, but I find it to be a horrible name.)

0

Share this post


Link to post
Share on other sites

 

 

extern int i = 14;
Works, if defined in the file where it is used.
It is confusing though, an extern token should only indicate declaration not definition.
I guess the compiler assumes that because there is an assignment, the extern keyword should be obviated, instead of throwing a compiler error.

 

It's not the compiler assuming anything, it's a rule in the standard. extern is not ignored. extern explicitly specifies the scope of the variable (which can be static, extern, auto, and register). That's why in global scope, you cant use extern and static within the same definition.  Most of the time it's not used, just like most of the time we don't write "signed int".

 

Actually, that is not true at all. Fir understood it right. extern simply imports a variable or function defined in another translation unit(code file), in other words it is a linkage process, the scope will stay file based, and the duration program-wise(some books call it static, but I find it to be a horrible name.)

 

The proper term is "storage class". Extern specifies a storage class. You're right that declaring a variable in a function with extern does not make that variable local. But it does mean that the variable must be defined elsewhere in the program with extern storage class.
 
Really there are four uses of extern in C:

extern int i; // declares a variable with external linkage.
 
extern int i=0; // defines a variable with external linkage, just like if extern wasn't there. A program can only have one definition of such a variable.
 
extern int foo(); //declares a function with external linkage. As above, extern can be and usually is omitted.

extern void foo(){} //defines a function with external linkage. As above function, except this is a definition. 

In the last three cases, extern can be replaced with static, to give the symbol internal linkage; extern and static are mutually exclusive. If you replace extern with static in the first case, it becomes a definition. Likewise extern cannot be used together with auto and register, the two storage classes of local variables. Consider:
 

void foo(){
  int i;//defines an "automatic" or stack variable.
  auto int j; //same as above. Not valid C++11. 
  register int k;//defines a register variable. Behaves the same as above, but the programmer intends the variable to be in a register. Same as above on widespread desktop compilers. 
  static int x; //defines a global variable, not accessible outside the function. 
  extern int y; //declares a global with external linkage.  
}

static int y;//Error. clashes with extern int y above.

In no case can the same variable or function have more than one storage class.

There is one other rule however:

static int i;
extern int i;//declares i with internal linkage, referring to the above definition.
Edited by King Mir
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0