hi
I am a bit new to this field. I have seen some open source os codes to be compiled with GCC. I found that many, almost all functions are declared as "static". I can understand static variables, but what are static functions?
please help me.
thank you.
"static" Functions
Re:"static" Functions
It makes the function inaccessible outside the file in which it is defined. http://www.phim.unibe.ch/comp_doc/c_man ... static.htm
Re:"static" Functions
Same as an anonymous namespace in C++. "This is not for the world, it's just for me here." Helps the linker, and helps the maintenance programmer.
Every good solution is obvious once you've found it.
Re:"static" Functions
Anonymous namespace in C++? Is there yet more C++ features that I've been missing? You surely don't mean the global namespace... so do you mean it's legal to say:
And then the result is the same as with 'static' qualifier: that stuff in other files (or namespaces I suppose) can't refer to the functions/variables with names (one can still use pointers ofcourse but..)?
Code: Select all
namespace {
/* this code in anynomous namespace? */
};
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:"static" Functions
Yes, that code is perfectly legal. Semantically, it's different than static though. Everything in an anonymous namespace still has external linkage AFAIK, but the namespace portion of the mangled names are randomly generated for each anonymous namespace. Because of this, it never makes sense to put an anonymous namespace in a header file, because it will create a different namespace for each translation unit into which it is included.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:"static" Functions
Yep. You can even have more than one of 'em.mystran wrote: ...so do you mean it's legal to say:
Code: Select all
namespace { /* this code in anynomous namespace? */ };
And the beauty of it is, it applies not only to functions, but classes, variables, and templates just as well.
Col. Kernel pointed out the downside, but that can usually be ignored safely.
Every good solution is obvious once you've found it.
Re:"static" Functions
Erm... while we're at it... you do know about anonymous blocks, do you?
Another trick to limit visibility (this time, of auto variable x). Note that x is properly destroyed at the end of each block.
Code: Select all
int main()
{
{
MyClass x;
foo(x);
}
{
MyClass x;
bar(x);
}
return 0;
}
Every good solution is obvious once you've found it.
Re:"static" Functions
Yes, ofcourse I do know about anonymous blocks. Known about those since .... about the time I started programming in C... which is a long time ago.
The funny thing is, at some point I used to write C code a lot like this:
I still remember the times when C didn't allow variable declarations midfunction; you had to have them all in the beginning, except if you used anonymous blocks. I actually think that allowing them midfunction tends to reduce the use of anonymous blocks, and pollute the intra-function namespace unnecessarily, so actually forcing the use of blocks wasn't necessarily as bad as it might seem.
Fortunately somebody at some point realized that if you declare variables within the first expression of a for-statement, then there's little point to let it exist in the surrounding scope.. except you still can't use it with plain C, unless you have C99 compiler (which GCC mostly is if you use a switch).
The funny thing is, at some point I used to write C code a lot like this:
Code: Select all
void foobar(void) {
/* do something */
{
int i;
for(i = 0; i < bar; i++) {
....
}
}
/* do something else */
}
Fortunately somebody at some point realized that if you declare variables within the first expression of a for-statement, then there's little point to let it exist in the surrounding scope.. except you still can't use it with plain C, unless you have C99 compiler (which GCC mostly is if you use a switch).