What is a “static” function in C?

前端 未结 12 1051
野的像风
野的像风 2020-11-22 16:03

The question was about plain c functions, not c++ static methods, as clarified in comments.

I understand what a static variable is, but wha

相关标签:
12条回答
  • 2020-11-22 16:22

    Minimal runnable multi-file scope example

    Here I illustrate how static affects the scope of function definitions across multiple files.

    a.c

    #include <stdio.h>
    
    /* Undefined behavior: already defined in main.
     * Binutils 2.24 gives an error and refuses to link.
     * https://stackoverflow.com/questions/27667277/why-does-borland-compile-with-multiple-definitions-of-same-object-in-different-c
     */
    /*void f() { puts("a f"); }*/
    
    /* OK: only declared, not defined. Will use the one in main. */
    void f(void);
    
    /* OK: only visible to this file. */
    static void sf() { puts("a sf"); }
    
    void a() {
        f();
        sf();
    }
    

    main.c

    #include <stdio.h>
    
    void a(void);        
    
    void f() { puts("main f"); }
    
    static void sf() { puts("main sf"); }
    
    void m() {
        f();
        sf();
    }
    
    int main() {
        m();
        a();
        return 0;
    }
    

    GitHub upstream.

    Compile and run:

    gcc -c a.c -o a.o
    gcc -c main.c -o main.o
    gcc -o main main.o a.o
    ./main
    

    Output:

    main f
    main sf
    main f
    a sf
    

    Interpretation

    • there are two separate functions sf, one for each file
    • there is a single shared function f

    As usual, the smaller the scope, the better, so always declare functions static if you can.

    In C programming, files are often used to represent "classes", and static functions represent "private" methods of the class.

    A common C pattern is to pass a this struct around as the first "method" argument, which is basically what C++ does under the hood.

    What standards say about it

    C99 N1256 draft 6.7.1 "Storage-class specifiers" says that static is a "storage-class specifier".

    6.2.2/3 "Linkages of identifiers" says static implies internal linkage:

    If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

    and 6.2.2/2 says that internal linkage behaves like in our example:

    In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function.

    where "translation unit" is a source file after preprocessing.

    How GCC implements it for ELF (Linux)?

    With the STB_LOCAL binding.

    If we compile:

    int f() { return 0; }
    static int sf() { return 0; }
    

    and disassemble the symbol table with:

    readelf -s main.o
    

    the output contains:

    Num:    Value          Size Type    Bind   Vis      Ndx Name
      5: 000000000000000b    11 FUNC    LOCAL  DEFAULT    1 sf
      9: 0000000000000000    11 FUNC    GLOBAL DEFAULT    1 f
    

    so the binding is the only significant difference between them. Value is just their offset into the .bss section, so we expect it to differ.

    STB_LOCAL is documented on the ELF spec at http://www.sco.com/developers/gabi/2003-12-17/ch4.symtab.html:

    STB_LOCAL Local symbols are not visible outside the object file containing their definition. Local symbols of the same name may exist in multiple files without interfering with each other

    which makes it a perfect choice to represent static.

    Functions without static are STB_GLOBAL, and the spec says:

    When the link editor combines several relocatable object files, it does not allow multiple definitions of STB_GLOBAL symbols with the same name.

    which is coherent with the link errors on multiple non static definitions.

    If we crank up the optimization with -O3, the sf symbol is removed entirely from the symbol table: it cannot be used from outside anyways. TODO why keep static functions on the symbol table at all when there is no optimization? Can they be used for anything?

    See also

    • Same for variables: https://stackoverflow.com/a/14339047/895245
    • extern is the opposite of static, and functions are already extern by default: How do I use extern to share variables between source files?

    C++ anonymous namespaces

    In C++, you might want to use anonymous namespaces instead of static, which achieves a similar effect, but further hides type definitions: Unnamed/anonymous namespaces vs. static functions

    0 讨论(0)
  • 2020-11-22 16:23

    Minor nit: static functions are visible to a translation unit, which for most practical cases is the file the function is defined in. The error you are getting is commonly referred to as violation of the One Definition Rule.

    The standard probably says something like:

    "Every program shall contain exactly one definition of every noninline function or object that is used in that program; no diagnostic required."

    That is the C way of looking at static functions. This is deprecated in C++ however.

    In C++, additionally, you can declare member functions static. These are mostly metafunctions i.e. they do not describe/modify a particular object's behavior/state but act on the whole class itself. Also, this means that you do not need to create an object to call a static member function. Further, this also means, you only get access to static member variables from within such a function.

    I'd add to Parrot's example the Singleton pattern which is based on this sort of a static member function to get/use a single object throughout the lifetime of a program.

    0 讨论(0)
  • 2020-11-22 16:23

    Since static function is only visible in this file. Actually, compiler can do some optimization for you if you declare "static" to some function.

    Here is a simple example.

    main.c

    #include <stdio.h>
    
    static void test() 
    {
        ghost(); // This is an unexist function.
    }
    
    int main()
    {
        int ret = 0;
    
    #ifdef TEST
    #else
        test();
    #endif
        return (ret);
    } 
    

    And compile with

    gcc -o main main.c
    

    You will see it failed. Because you even not implement ghost() function.

    But what if we use following command.

    gcc -DTEST -O2 -o main main.c
    

    It success, and this program can be execute normally.

    Why? There are 3 key points.

    1. -O2 : Compiler optimization level at least 2.
    2. -DTEST : Define TEST, so test() will not be called.
    3. Defined "static" to test().

    Only if these 3 conditions are all true, you can pass compilation. Because of this "static" declaration, compiler can confirm that test() will NEVER be called in other file. Your compiler can remove test() when compiling. Since we don't need test(), it does not matter whether ghost() is defined or implemented.

    0 讨论(0)
  • 2020-11-22 16:24

    The following is about plain C functions - in a C++ class the modifier 'static' has another meaning.

    If you have just one file, this modifier makes absolutely no difference. The difference comes in bigger projects with multiple files:

    In C, every "module" (a combination of sample.c and sample.h) is compiled independently and afterwards every of those compiled object files (sample.o) are linked together to an executable file by the linker.

    Let's say you have several files that you include in your main file and two of them have a function that is only used internally for convenience called add(int a, b) - the compiler would easily create object files for those two modules, but the linker will throw an error, because it finds two functions with the same name and it does not know which one it should use (even if there's nothing to link, because they aren't used somewhere else but in it's own file).

    This is why you make this function, which is only used internal, a static function. In this case the compiler does not create the typical "you can link this thing"-flag for the linker, so that the linker does not see this function and will not generate an error.

    0 讨论(0)
  • 2020-11-22 16:26

    There is a big difference between static functions in C and static member functions in C++. In C, a static function is not visible outside of its translation unit, which is the object file it is compiled into. In other words, making a function static limits its scope. You can think of a static function as being "private" to its *.c file (although that is not strictly correct).

    In C++, "static" can also apply to member functions and data members of classes. A static data member is also called a "class variable", while a non-static data member is an "instance variable". This is Smalltalk terminology. This means that there is only one copy of a static data member shared by all objects of a class, while each object has its own copy of a non-static data member. So a static data member is essentially a global variable, that is a member of a class.

    Non-static member functions can access all data members of the class: static and non-static. Static member functions can only operate on the static data members.

    One way to think about this is that in C++ static data members and static member functions do not belong to any object, but to the entire class.

    0 讨论(0)
  • 2020-11-22 16:28

    static function definitions will mark this symbol as internal. So it will not be visible for linking from outside, but only to functions in the same compilation unit, usually the same file.

    0 讨论(0)
提交回复
热议问题