Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a header file a1.h which looks like this
a1.h

static int sum;
int data;

Now i am including this a1.h in two .cpp files one.cpp and two.cpp.
Questions:
1.Will both the files share the sum variable or each will have its own copy?
2.what is the difference between sum and data in terms of memory allocation when a1.h is included in both the files?
3.dont static variables have a file scope, so how is it accessible in other files?
Posted

You really shouldn't put that stuff in the header file.
That said, I guess you'll get a linker error for the data variable (multiple symbol definitions) while each source file will get its own copy of the sum variable.
 
Share this answer
 
v2
Comments
Abhinay Kumar 13-Feb-12 5:16am    
since i am including the file i am not getting any linking error.
CPallini 13-Feb-12 5:23am    
I suppose it is your (or your compiler's) fault.
Please detail your code and report info about your build environment.
After the guess, I did a test (with VS2005), getting the link error:
'error LNK2005: "int data" (?data@@3HA) already defined in [...]'
Abhinay Kumar 13-Feb-12 7:00am    
yes correct after i did it in eclipse i got the error but in turbo C++ i wasnt somehow getting the error.
Abhinay Kumar 13-Feb-12 7:15am    
cant i access data in both the files using extern declaration.How to do it?
CPallini 13-Feb-12 7:58am    
If you need to access the variable defined in one source file (say 'one.cpp') from another source file (say 'two.cpp') then you may:
(1) define it in one.cpp: int data;
(2) declare it as extern in two.cpp: extern int data;
The best you can do is try and compile it, and see what happens.
Good luck,
 
Share this answer
 
Comments
Abhinay Kumar 13-Feb-12 4:53am    
This a silly answer.I came here after compiling these programs.
CPallini 13-Feb-12 5:36am    
After a question like yours, there's no silly answer.
Pablo Aliskevicius 14-Feb-12 2:06am    
Dear Abhinay,
Based on your question, I assume that you are new to C/C++. First and foremost, welcome on board!
Second, since you're on CodeProject since 2008, I assume you're not new to programming. In your profile, you don't say much about yourself, so anyone trying to help you will have to guess - I guess you have a background in either .NET or Java, or possibly Visual Basic.
If this is true, I can recommend a site where you can read about C++, you can find there a good book that is meant for programmers trying to learn C++ after knowing another language: http://www.mindviewinc.com/Books/downloads.html, the book names are "Thinking in C++, 2nd edition, Volume 1" and "Thinking in C++, Volume 2: Practical Programming". I have recommended those books to colleages coming to C++ from C and from C#, and they have found them interesting.
Of course, if my guesses are wrong and you have a background in C++, you should totally ignore this post.
Again, good luck,
1) Each file will have its own "sum" variable, and hence memory will be allocated twice, because it is declared as static (file scope).
2) "data" has global scope hence both *.c files will compile but linker will fail with "multiply defined symbol"-type error.
3) There is no way to share static variables across files (unless you start doing something really unorthodox - e.g. you could work out the address in memory where the variable will end up - but this will be just plain bad practice). Moreover, there is no need to share static variables and/or functions as the only reason you declared them as static is to prevent global scope.
4) You should not define variables or any objects for that matter in header files. Declare them in header files and then instantiate in c files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900