why static data cannot be accessed from other programs file

H

hayyal

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.
 
J

Joe Greer

(e-mail address removed) wrote in @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
 
N

nagaraj_hayyal

(e-mail address removed) wrote in @k79g2000hse.googlegroups.com:




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.


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.


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.




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

joe

Thanks Joe :)
 
J

James Kanze

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 the standard says so. Basically, it says that if a
variable is declared static, it has internal linkage, i.e. the
use of that name in another translation unit refers to another
entity.
whats the difference between global and static variables?

There's no such thing as global variables:).

Seriously, there are at least three distinct concepts which seem
to be involved here:

-- The keyword static. Depending on how and where it is used,
this affects either the linkage, the lifetime, or both.
(The usual use of the word "static", other than as a
keyword, would refer to lifetime, and not linkage.) Thus,
for example, the effect of declaring a variable with local
scope static is different than the effect of declaring one
with namespace scope static.

-- The lifetime. C++ distinguished several different lifetimes
of variables: the three principal ones are static, auto and
dynamic (but temporary objects and exceptions have other
lifetimes). Roughly speaking, a static object lives for the
lifetime of the program (with some exceptions---but in no
case will it's lifetime end before the function exit() is
called). An auto object ceases to exist when the program
leaves the block in which it was declared, and the lifetime
of dynamic objects is entirely in the hands of the
programmer.

-- The linkage. This basically concerns whether two uses of
the same identifier refer to the same entity or not, and is
a characteristic of the identifier, not of the entity to
which it refers. (Also, things like type names or enum
constants have linkage, although you can't really speak of
their lifetime.) In C++, an identifier has "external
linkage" (all identical identifiers with external linkage
refer to the same entity, even accross translation unit
boundaries), "internal linkage" (identical identifiers with
internal linkage refer to the same entity within a single
translation unit, but refer to different entities when they
appear in different translation units), or "no linkage"
(each occurance of the identifier refers to a separate
entity). (In the above, "identifier" is always the fully
qualified identifier---two identical symbols declared in
different scopes are different identifiers.)

So before you can compare static with something else, you have
to specify what you mean by it---if you mean the keyword, you
have to specify where it occurs, because its effect varies: on a
variable at namespace scope, it changes the linkage from
exteral to internal, whereas on a local variable, it changes the
lifetime from auto to static. (All variables declared at
namespace scope have static lifetime.)

Also, note that the use of static on entities at namespace scope
has been deprecated in C++; it is considered preferable to use
an anonymous namespace.
Both these variables behave almost in similar manner, then where is
the memory allocated for these variables?

Where ever the compiler wants. If you're talking about
variables declared static at namespace scope, all it means is
that the same identifier refers to two different entities, so
the compiler will allocate the memory for two different
variables, in exactly the same way as it would if they had
different names.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top