Jump to content
  • Advertisement
Sign in to follow this  
DvDmanDT

static functions?

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is a static function? I think I know what a static method is, but not a function.. So what is it? And does it automaticly make the function threadsafe or what? I've seen people wrapping entire libraries with static functions, and then call them threadsafe.. :p So, what are they, and when/why should I use them?

Share this post


Link to post
Share on other sites
Advertisement
A static function is identical to a static method. Method is the Java (and similar languages) way of saying it, and function is the C++ (and similar) way of saying it.

Edit: In abstract compsci terms, of course. Who can say what languages can be found in the wild for which a "static function" means something entirely different?

Share this post


Link to post
Share on other sites
Static function are for use in classes. Say you have a value you need to retrieve, but you don't have the object that member function is in. You can make it static and call name = objectsname::staticfunctionsname();

Share this post


Link to post
Share on other sites
In C, the static declaration typically indicates that a function (or variable) is only accessible from other functions in that same file (aka translation unit). The prefix tells the compiler not to form information that the linker uses to reference functions across .obj files. This can be worked around by using the extern keyword with a prototype of a function that is declared static but that another file needs to use. For example, if function X is declared static in file A, then other functions in file A can call it, but functions in file B can't, unless at some place in file B a prototype of function X is declared using the extern keyword. This happens outside of using header files to impart function prototypes across files. For a variable, the static keyword tells the linker to provide space for the variable in the actual program file itself. The static keyword probably acts in a similar way for C++. // outside of classes that is ...

Are you certain that those libraries used the static keyword alone to ensure thread safety? Maybe other mechanisms were at work, but just weren't obvious.

Share this post


Link to post
Share on other sites
Ok, so it's only for visibility then?

Yes, in C..

I can't give you an example, and no, I'm not sure that's all they do, but I know alot of projects only declare functions as static when threadsafe is enabled..

Share this post


Link to post
Share on other sites
Since you are talking about static functions and methods, I've assumed you're talking about C++.

The keyword 'static' is a storage class specifier. Functions occupy a position or address on the stack just like stack-based variables. This is why you can use such things as function pointers and function indirection.

Static member functions of classes are used to produce 'per class' behaviour, as opposed to the usual 'per instance behavior'. This is important when members maintain state, and where you do not want this state to change from instance to instance, so you would make these superclass member functions 'static'. A static member function will not have a 'this' pointer, but still remains in the scope of the class and subclasses.

For general use with functions in C++, I'll quote from http://zach.chambana.net/apache-cplusplus/cplusplus/static.html

"Stroustrup (3rd ed.), page 200 says:

In C and older C++ programs, the keyword static is (confusingly) used to mean "use internal linkage" (Section B.2.3). Don't use static except inside functions (Section 7.1.2) and classes (Section 10.2.4).

Stroustrup (3rd ed.), page 819 says:

The keyword static, which usually means "statically allocated," can be used to indicate that a function or an object is local to a translation unit. For example:

// file 1:
static int glob;

// file 2:
static int glob;

This program genuinely has two integers called glob. Each glob is used exclusively by functions defined in its translation unit.

The use of static to indicate "local to translation unit" is deprecated in C++. Use unnamed namespaces instead (Section 8.2.5.1)."

Hope that helps.

--random

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
It has nothing to do with thread safety, only with scoping.


Quick question...I have read about this in connection with being threadsafe, but maybe it was wrong as I am not an expert on threads. I have heard that static class functions and non-const static variables in a superclass are not threadsafe when used by subclasses. Is this true?

--random

Share this post


Link to post
Share on other sites
They are only as thread safe as global functions using global variables. There is no one answer, but in general they are not thread safe when used by anything, not just derived classes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!