"static" Functions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
The Tree

"static" Functions

Post 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.
AR

Re:"static" Functions

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:"static" Functions

Post 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.
Every good solution is obvious once you've found it.
mystran

Re:"static" Functions

Post 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..)?
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:"static" Functions

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:"static" Functions

Post 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.
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:"static" Functions

Post 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.
Every good solution is obvious once you've found it.
mystran

Re:"static" Functions

Post 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).
Post Reply