What is the point of making a function static in C?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Making a function
static
hides it from other translation units, which helps provide encapsulation.helper_file.c
main.c:
pmg is spot on about encapsulation; beyond hiding the function from other translation units (or rather, because of it), making functions
static
can also confer performance benefits in the presence of compiler optimizations.Because a
static
function cannot be called from anywhere outside of the current translation unit (unless the code takes a pointer to its address), the compiler controls all the call points into it.This means that it is free to use a non-standard ABI, inline it entirely, or perform any number of other optimizations that might not be possible for a function with external linkage.
C programmers use the static attribute to hide variable and function declarations inside modules, much as you would use public and private declarations in Java and C++. C source files play the role of modules. Any global variable or function declared with the static attribute is private to that module. Similarly, any global variable or function declared without the static attribute is public and can be accessed by any other module. It is good programming practice to protect your variables and functions with the static attribute wherever possible.
pmg's answer is very convincing. If you would like to know how static declarations work at object level then this below info could be interesting to you. I reused the same program written by pmg and compiler it into a .so(shared object) file
Following contents are after dumping the .so file into something human readable
0000000000000675 f1: address of f1 function
000000000000068c f2: address of f2(staticc) function
note the difference in the function address , it means something . For a function that's declared with different address , it can very well signify that f2 lives very far away or in a different segment of the object file.
Linkers use something called PLT(Procedure linkage table) and GOT(Global offsets table) to understand symbols that they have access to link to .
For now think that GOT and PLT magically bind all the addresses and a dynamic section holds information of all these functions that are visible by linker.
After dumping the dynamic section of the .so file we get a bunch of entries but only interested in f1 and f2 function.
The dynamic section holds entry only for f1 function at address 0000000000000675 and not for f2 !
Num: Value Size Type Bind Vis Ndx Name
And thats it !. From this its clear that the linker will be unsuccessful in finding the f2 function since its not in the dynamic section of the .so file.
Looking at the posts above I would like to point one detail.
Suppose our main file ("main.c") looks like this:
Now consider three cases:
Case 1: Our header file ("header.h") looks like this:
Then the following command on linux:
will succeed! Following that if one runs
The output will be
Calling function inside header
Which is what that static function should print.
Case 2: Our header file ("header.h") looks like this:
and we also have one more file "header.c", which looks like this:
Then the following command
will give an error.
Case 3:
Similar to case 2, except that now our header file ("header.h") is:
Then the same command as in case 2 will succeed, and further executing ./main will give the expected result.
So from these tests (executed on Acer x86 machine, Ubuntu OS) I made an assumption that
static keyword prevents function to be defined in another file than where it is declared.
Correct me if I am wrong.
The
static
keyword in C is used in a compiled file (.c as opposed to .h) so that the function exists only in that file.Normally, when you create a function, the compiler generates cruft the linker can use to, well, link a function call to that function. If you use the static keyword, other functions within the same file can call this function (because it can be done without resorting to the linker), while the linker has no information letting other files access the function.