'static' to make object accessible by file

L

Lax

Say we have a static external object (object defined outside of any
block with static qualifier) in a file.
We are telling the compiler that we intend that object to be accessed
by functions in the current file that are below the definition.

Now what if one of these functions passes a pointer value to this
object to a function in another file, and that function uses the
pointer to access this static object? Is this 'normal' or dependent on
the specific environment?

This works on my system but I am not sure if this is a normal thing I
should be doing or it's just because my environment/compiler isn't
picking up on the bad move. If it's okay, is it bad style?

So, does the C standard say that such a move is allowed (either
directly or indirectly by not saying that it is disallowed)?
 
I

Ian Collins

Lax said:
Say we have a static external object (object defined outside of any
block with static qualifier) in a file.
We are telling the compiler that we intend that object to be accessed
by functions in the current file that are below the definition.

Now what if one of these functions passes a pointer value to this
object to a function in another file, and that function uses the
pointer to access this static object? Is this 'normal' or dependent on
the specific environment?

Normal. A static object does not have global linkage, but it still has
to exist somewhere.
 
W

Walter Roberson

Lax said:
Say we have a static external object (object defined outside of any
block with static qualifier) in a file.
We are telling the compiler that we intend that object to be accessed
by functions in the current file that are below the definition.
Now what if one of these functions passes a pointer value to this
object to a function in another file, and that function uses the
pointer to access this static object? Is this 'normal' or dependent on
the specific environment?

That is normal and allowed. There are lurking dangers, such as if
the static data is declared const but the routines do not consistantly
use the const qualifier and ending up modifying the data.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top