Static Data

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
shad

Static Data

Post by shad »

When is it good to use a static data type? I know what it is in a general programming sense, but in osdev whats the reason its used in alot of source code i see?
_mark

Re:Static Data

Post by _mark »

The only reason would be that you could use the same symbol name (for different symbols) in different modules and not have to think up a new clever name. Me on the other hand, I have a nameing scheme I follow that avoids the need for such things. A quick search of "static" in all my sources yeilded nothing but some stuff in comments.

In general programming though, there are very good reasons to use them. And in C++ static instanciation of classes can lead to some really cool factories and other clever things.

statics have there strongest use when you have many programmers working on the same project. This in general just protects them from one another - or if your project is rather large I guess it could protect you from yourself.

_mark()
Tim

Re:Static Data

Post by Tim »

The static keyword is used for several different purposes in C and C++:

static global functions and variables are private to that particular source file. Unless you have a strict naming convention, you should make any variable or function that is only used within that file static.

static local variables are similar to static global variables, except that they can only be used by the function where they are defined.

static class member functions and variables are similar to global functions and variables. Although they're declared within a class {} declaration, they aren't associated with a particular instance of the class and they can't access any non-static member functions or variables.
Post Reply