questions about static function & static const variable

This is a discussion on questions about static function & static const variable within the C Programming forums, part of the General Programming Boards category; I have some questions (actually two) that really bugging me for a long time. question #1: why are some functions ...

  1. #1
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82

    questions about static function & static const variable

    I have some questions (actually two) that really bugging me for a long time.

    question #1:
    why are some functions declared static?
    what is the effect of declaring a function static?
    Code:
    static void foo (void)
    {
      /* blah blah blah... */
    }
    question #2:
    is this function thread safe?
    Code:
    int foo (int bar)
    {
      static const int a_static_const_int = 100;
      int foobar;
    
      /* blah blah blah... */
    
      return(foobar);
    }
    thank you for your attention.
    Last edited by cph; 01-19-2009 at 12:08 AM.

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    why are some functions declared static?
    Such functions cannot be seen outside of their translation unit (which essentially means source file). If you're not sharing a function with other .c files, then you should mark it as static so it's local only to that file. The same goes for global variables you don't want to share.
    is this function thread safe?
    Probably not, depending on what you actually do with the static data. It might be, if you carefully lock access to it, but it's better to try to avoid using static there.

    The keyword "static" is overloaded in C. At file scope (that is, globally), it means "hide this from other source files". Inside of a function, it means "this variable retains its value between calls" (that is, it essentially becomes global but accessible only in that particular block).

    To make life even more fun, C99 added a new meaning for static, but it's not really all that useful to know about it.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,040
    I disagree with cas on question 2. The variable is constant. It will never change and always be initialized.

    That is thread safe. But the convention for constants is to be in ALL_CAPS format so that they stand out.
    Last edited by King Mir; 01-19-2009 at 12:51 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by King Mir View Post
    I disagree with cas on question 2. The variable is constant. It will never change and always be initialized.

    That is thread safe. But the convention for constants is to be in ALL_CAPS format so that they stand out.
    I agree with King Mir, even though the source code example doesn't even show any interaction with the static const.
    As it is, it is undeniably thread safe, and the question was implied to be about the exact code posted, rather than about a function which does something like what he posted. The static const might even be optimised out in this case, if it isn't used.
    However, it is returning an uninitialised variable which kinda makes it pointless. In other words, as the question about the real code, not an unrepresentative mock example.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    That is thread safe. But the convention for constants is to be in ALL_CAPS format so that they stand out.
    It is normally only macros that should be all caps. Constants may be marked in a special way to indicate that they are constants.

    Also, to clarify the points made:
    a static const variable should never be changed by the compiler, so it should be safe to use in a multithreaded environment.

    On the other hand, if you have a static variable that is NOT const, it's a BIG warning sign for multithreaded code - a static local variable is essentially a global variable which can't be seen outside of the current function. So if two threads are using the function at the same time, they would potentially modify the very same variable. One particular "trick" where static variable is often used is where a C style string is returned, and a static char array is used to store the string - this will NOT be thread-safe, as another call to the same function will modify the string returned by another call.

    --
    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
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    okay okay, is this function thread-safe?
    Code:
    double circle_area (double radius)
    {
      static const double the_pi = 3.1415926535897932384626433832795;
    
      return(the_pi * radius * radius);
    }
    ps : please do not complain about the lack of error checking

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes.

  8. #8
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    Yipes, I completely missed the "const" in the second example, even though the variable name itself included the word "const", too. Mea culpa.

  9. #9
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    okay, thank you very much for the answers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21