-
scope question
I have been in a 'never done this before' position recently where i have had to train somebody up in programming 'fundamentals' in order for her to be able to maintain some code.
One thing that cropped up as a query point was scope - I explained local and global - but then i thought what is the 'in between' ?is there any other description? Are all data local - reading back to main() - Is the only difference a global?
[EDIT] I suppose its all relative to the operating system in the end
-
Scope is a context in which something is declared to exist and/or its name is visible to the compiler.
This covers type declarations and definitions (can't use a type declaration unless it is visible to the compiler within the current scope) as well variable definitions (can't use a variable unless a declaration of it is visible to the compiler, and it is defined exactly once somewhere).
Every block (code other than declarations that appears between curly braces) is associated with a scope. This is why auto (what you're calling "local") variables cease to exist - as far as the program is concerned, unless it exhibits undefined behaviour - when execution passes out of the block in which they are defined.
All class and struct types are also associated with a scope, that has the same name as the class/struct type. A namespace is a scope. That is why class and namespace members - whether static or otherwise - are identified using class_name::member_name. It is not an accident that the "::" operator is called the scope operator. If the member is static, then class_name::member_name uniquely identifies that member. If the member is non-static, it can only be used within context of an instance of the class (i.e. it can't exist without being associated with an object of type class_name). So some_object.nonstatic_member_name is functionally equivalent to some_object.class_name::nonstatic_member_name.
Global variables are simply variables who's scope of existence is associated with execution of the entire program - they come into existence when a program starts (potentially even before main() is called) and cease to exist when the program terminates (potentially after main() returns).
Scopes can enclose other scopes.
-
Code:
extern int global;
static int fileLocal;
void foo ( int param ) {
int a;
static int b;
for ( int i = 0 ; i < param ; i++ ) {
int a;
}
a = param;
}
global is visible to everything in the current process
fileLocal is visible (by name) only to functions in the current source file.
a (the first one) is a local variable
b is local to foo only, but it's value is preserved across function calls.
i and a (the other one) have a scope limited to the for loop block itself.
static variables (in one file) can be used in other files by passing pointers around (eg. asctime())
In C++, you also have namespaces, so things like this are possible.
Code:
namespace foo {
int a;
}
namespace bar {
int a;
}
int main()
{
foo::a = 1;
bar::a = 2;
}
> I suppose its all relative to the operating system in the end
You can add to the above say
- shared memory, which persists beyond the life of a single process (until the last process sharing it exits)
- files, which persist beyond the run-time of the OS.
-
Thanks all, i particlularly liked Salem's example , with the 'layers' of scope illustrated, OS 'scope' i liked too.