Thread: Local function keyword

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Local function keyword

    What does the LOCAL keyword do in the following function? I am assuming it makes the function "foo" only available to other functions inside the same c file, but just looking for someone to confirm that. Thanks!

    Code:
    # define LOCAL static
    
    LOCAL void foo(int bar)
        {
            ...do something...
        }
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mlupo View Post
    What does the LOCAL keyword do in the following function? I am assuming it makes the function "foo" only available to other functions inside the same c file, but just looking for someone to confirm that.
    In. It seems kind of superfluous here.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    yeah, I agree. But this is a production system that I'm making a change to and I'm kind of (ok VERY) rusty with my C.
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mlupo View Post
    yeah, I agree. But this is a production system that I'm making a change to and I'm kind of (ok VERY) rusty with my C.
    I don't know if it would work (you could easily test the premise) but I suppose someone might have done this so they could later optionally
    Code:
    #define LOCAL NULL
    to unstatic those functions. Depending on the whole context
    Code:
    #define LOCAL extern
    might be a possibility too, depending on the places where LOCAL is being used.

    I would guess it is left over from some development stage.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It doesn't buy you much.

    It might be a means to disambiguate other uses of static, for example
    Code:
    static int moduleVar;
    LOCAL void foo ( int bar ) {
      static int privateAndPersistentInFoo;
    }
    Or maybe someone long ago had a tool which did 'grep LOCAL' to find stuff.

    How old is the code, does it pre-date ANSI-C (1990 or so)?
    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.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Probably not. See the type declaration inside the parentheses? And why did the writer didn't grep the program itself for LOCAL or sed LOCAL to static? I think it's also bad style to "hide" keywords.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by Brafil View Post
    Probably not. See the type declaration inside the parentheses? And why did the writer didn't grep the program itself for LOCAL or sed LOCAL to static? I think it's also bad style to "hide" keywords.
    The programmer probably just kept forgetting what static stood for, since it is used in many different ways. Also, many more people know grep than sed. Maybe the programmer didn't know how to replace with
    Code:
    sed -i 's/LOCAL/static/g' file_name.cpp

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    LOCAL may in fact mean something more than just static. For instance, you may want to use a different calling convention (fast/register call for instance) for functions which are not exposed to other modules. So LOCAL might actually expand to something like "static __fastcall" or some such.

    The purpose is not to hide the "static" keyword but to allow the flexibility to quickly change some aspect of the declaration of ALL local functions.

    This is common practice.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Oh, than you're right. It'd be better than to see "static __fastcall ... void function(void)" everywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 3
    Last Post: 05-24-2009, 02:42 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Replies: 18
    Last Post: 12-31-2005, 01:56 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