external variables in a multifile program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

external variables in a multifile program

 
0
  #1
Jul 28th, 2005
I have a question on external variables in a multifile program.
Quoting from K&R (first addition, page 72)

"By default, external variables are also "global", so that all references to such a variable by the same name (even from functions compiled separately) are references to the same thing."

Here's an example that doesn't work. The variable 'a' in file 1 is external (since it's defined outside of any function) so, according to the above paragraph, it should have the same value in file 2. The
program, however, doesn't compile. If the line "int a" is added to
file 2 it compiles and runs, giving the wrong answer 0. If instead the
line "extern int a" is added then it runs and gives the correct
answer 12. I understand why the last version works, but shouldn't
the first one also (according to K&R)?

Does anyone have any insights to offer?

_____________________file 1____________________________
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int a=6;
  5. int f(int);
  6. main()
  7. { cout << f(2) << endl;
  8. }
________________file 2____________________________
  1. int f(int x)
  2. { return (a*x);
  3. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: external variables in a multifile program

 
0
  #2
Jul 28th, 2005
Originally Posted by murschech
Does anyone have any insights to offer?
I'll try.

Originally Posted by murschech
Here's an example that doesn't work. The variable 'a' in file 1 is external (since it's defined outside of any function) so, according to the above paragraph, it should have the same value in file 2. The program, however, doesn't compile.
As it shouldn't. Identifiers need to be declared. The linkage of an identifier is a bit separate.

Originally Posted by murschech
If the line "int a" is added to file 2 it compiles and runs, giving the wrong answer 0.
I get a linker error because adding this definition conflicts with the definition in the other file.

Originally Posted by murschech
If instead the line "extern int a" is added then it runs and gives the correct answer 12. I understand why the last version works, but shouldn't the first one also (according to K&R)?
I'll borrow Chris Torek's words of wisdom: "At file scope, 'extern' means, approximately, 'read this as a declaration, not a definition'".


[aside]Using K&R1 as a reference for C++98 seems to leave room for a number of gaps.[/aside]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC