Page 1 of 1
"static" Functions
Posted: Tue Jul 19, 2005 2:52 am
by The Tree
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.
Re:"static" Functions
Posted: Tue Jul 19, 2005 3:12 am
by AR
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
Posted: Tue Jul 19, 2005 6:12 am
by Solar
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.
Re:"static" Functions
Posted: Tue Jul 19, 2005 1:41 pm
by mystran
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:
Code: Select all
namespace {
/* this code in anynomous namespace? */
};
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..)?
Re:"static" Functions
Posted: Tue Jul 19, 2005 2:28 pm
by Colonel Kernel
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.
Re:"static" Functions
Posted: Wed Jul 20, 2005 1:25 am
by Solar
mystran wrote:
...so do you mean it's legal to say:
Code: Select all
namespace {
/* this code in anynomous namespace? */
};
Yep. You can even have more than one of 'em.
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.
Re:"static" Functions
Posted: Wed Jul 20, 2005 6:26 am
by Solar
Erm... while we're at it... you
do know about anonymous blocks, do you?
Code: Select all
int main()
{
{
MyClass x;
foo(x);
}
{
MyClass x;
bar(x);
}
return 0;
}
Another trick to limit visibility (this time, of auto variable x). Note that x is properly destroyed at the end of each block.
Re:"static" Functions
Posted: Wed Jul 20, 2005 12:46 pm
by mystran
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:
Code: Select all
void foobar(void) {
/* do something */
{
int i;
for(i = 0; i < bar; i++) {
....
}
}
/* do something else */
}
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).