Thread: scope question

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    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
    Last edited by rogster001; 03-02-2013 at 05:09 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Thanks all, i particlularly liked Salem's example , with the 'layers' of scope illustrated, OS 'scope' i liked too.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about scope
    By _Mike in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2011, 10:09 PM
  2. Question about scope...
    By CommonTater in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2011, 05:05 AM
  3. Question on scope
    By Kiklion in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2009, 03:15 AM
  4. Question About Scope
    By Kernel Sanders in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2008, 07:09 PM
  5. Scope Question
    By Vylrath in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2006, 02:53 PM