Thread: controlling modifying global variable

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    controlling modifying global variable

    Hi.

    I am coding in C.

    i have a global variable.

    I want only one function to change its value, while all other functions could read the value in global variable, But they are not allowed to change the value of global variable.

    Can any one suggest, what is the way to do it.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am coding in C.
    This thread has been moved to the C programming forum.

    i have a global variable.

    I want only one function to change its value, while all other functions could read the value in global variable, But they are not allowed to change the value of global variable.

    Can any one suggest, what is the way to do it.
    Use a local variable instead, and only pass it by value to all other functions except that one function, where you pass its address instead of its value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As laserlight is implying, there is no way that you can access-control a global variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If it's a struct, you might be better off passing it as a const pointer foe efficiency's sake. Const means they can't change it.
    You could make the functions accept a const argument anyway to catch any errors trying to modify the variable when they shouldn't.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    This thread has been moved to the C programming forum.


    Use a local variable instead, and only pass it by value to all other functions except that one function, where you pass its address instead of its value.
    Of course, it doesn't have to be a local variable, just as long as you pass it to code that doesn't have the right to access it directly, and either use pass by value or by const pointer (for larger pieces of data pass by pointer is more efficient, and const will ensure that your code doesn't accidentally write to the data).

    Of course, if you make it global, there's still the chance that some code will access the variable AS A GLOBAL, which can only be detected by code inspection [or randomly changing the name of the variable and the allowed changes and see what breaks!]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If it's a struct, you might be better off passing it as a const pointer foe efficiency's sake. Const means they can't change it.
    Ah yes, I had built-in types in mind. That said, I would clarify it as having a pointer to const parameter, or a pointer to const that is itself const, since const pointer usually means pointer that is const instead of pointer to const.

    Of course, it doesn't have to be a local variable, just as long as you pass it to code that doesn't have the right to access it directly
    That seems like a non-solution though since it disallows by convention without enforcing the convention in code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Ah yes, I had built-in types in mind. That said, I would clarify it as having a pointer to const parameter, or a pointer to const that is itself const, since const pointer usually means pointer that is const instead of pointer to const.
    Of course, you're right. I'm always think about how it's written like "const type*" (could be read like const pointer) rather than "type* const" (could be read like pointer to const). Just a little confusing
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    That seems like a non-solution though since it disallows by convention without enforcing the convention in code.
    Yes, I agree. However, we may have a situation where a variable needs to have global semantics. One way to "hide it", then would be to use a static local variable, and return a pointer to it:
    Code:
    struct blah
    {
       int x, y;
    };
    
    struct blah *getBlah()
    {
        static blah b;
        return &b;
    }
    
    // This function can modify blah. 
    void modifyblah(struct blah *b)
    {
       b->x++;
       if (b->x > b->y) 
          b->y++;
    }
    
    void useblah(const struct blah *b)
    {
         ... some code that uses b, but not allowed to modify it
    }
    
    int main()
    {
       struct blah *b;
    
       b = getBlah();
       while(1) {
          modifyblah(b);
          useblah();
       }
    }

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could also hide a global variable inside a source file and use access functions. One for read, one for write. Functions can call appropriate functions to get a pointer to the global variable (const for read).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You could also hide a global variable inside a source file and use access functions. One for read, one for write. Functions can call appropriate functions to get a pointer to the global variable (const for read).
    Yes, and to avoid accidental use elsewhere, make it a static global variable, so only those functions in that file can actually see the variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding global variable.
    By sunny_master_07 in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2008, 06:38 AM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  4. global variable turns zero
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 12:52 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM