473,387 Members | 1,844 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,387 software developers and data experts.

why static data cannot be accessed from other programs file

Hi Folks,

Just wanted to know few things about static variables.

why static variables cannot be accessed in other files by using extern
in c/c++?
whats the difference between global and static variables?
Both these variables behave almost in similar manner, then where is
the memory allocated for these variables?

Appreciate your comments/suggestions.

Thanks,
Nagaraj Hayyal.

Oct 11 '07 #1
2 1701
ha****@alcatel-lucent.com wrote in news:1192113564.121112.142920
@k79g2000hse.googlegroups.com:
Hi Folks,

Just wanted to know few things about static variables.

why static variables cannot be accessed in other files by using extern
in c/c++?
Because that is the way they are defined. Basically, a static variable
doesn't have external linkage. That is, no symbolic information is
stored in the resultant object file to allow use by other files. This
is to keep a check on global name pollution. In C++, the preferred way
to do this is with anonymous namespaces, but C didn't/doesn't have
namespaces.
whats the difference between global and static variables?
Global variables are accessible throughout the program, static variables
are not. Also, global variables can't be defined within a function, but
can be accessed via an extern declaration scoped within a function.
Both these variables behave almost in similar manner, then where is
the memory allocated for these variables?
This would be implementation dependent, but traditionally statics and
globals are kept in separate memory spaces but neither is on the heap.
As I said before, the difference is primarily in whether the variable is
accessible outside the current translation unit (or .c/.cpp) file.
>
Appreciate your comments/suggestions.
Hope that helps and isn't too far from what the actual spec says. :)

joe
Oct 11 '07 #2
On Oct 11, 8:22 pm, Joe Greer <jgr...@doubletake.comwrote:
hay...@alcatel-lucent.com wrote in news:1192113564.121112.142920
@k79g2000hse.googlegroups.com:
Hi Folks,
Just wanted to know few things about static variables.
why static variables cannot be accessed in other files by using extern
in c/c++?

Because that is the way they are defined. Basically, a static variable
doesn't have external linkage. That is, no symbolic information is
stored in the resultant object file to allow use by other files. This
is to keep a check on global name pollution. In C++, the preferred way
to do this is with anonymous namespaces, but C didn't/doesn't have
namespaces.
whats the difference between global and static variables?

Global variables are accessible throughout the program, static variables
are not. Also, global variables can't be defined within a function, but
can be accessed via an extern declaration scoped within a function.
Both these variables behave almost in similar manner, then where is
the memory allocated for these variables?

This would be implementation dependent, but traditionally statics and
globals are kept in separate memory spaces but neither is on the heap.
As I said before, the difference is primarily in whether the variable is
accessible outside the current translation unit (or .c/.cpp) file.
Appreciate your comments/suggestions.

Hope that helps and isn't too far from what the actual spec says. :)

joe
Thanks Joe :)

Oct 11 '07 #3

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

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
4
by: raghavendra | last post by:
Hi , A static member can be accessed only by another static method....but the vice-versa is not true....Can anyone pls explain me the logic behind this... Also in a project, if we have too many...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
3
by: Phillip Ian | last post by:
Just a quick architecture question. I'm just looking for discussion, not a flame war, please. In the past, I've tended to use a public module for my data layer functions. Something like: ...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: sandeep | last post by:
Hi why we cannot have static as a structure member? & also is there any way to achive data hiding in C at this level( i.e. access only selected structure member ) following code gives syntax...
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.