On Thu, Aug 7, 2008 at 4:04 PM, Thomas Hruska <[EMAIL PROTECTED]> wrote:
> John Matthews wrote:
>> --- In [email protected], kathir resh <[EMAIL PROTECTED]> wrote:
>>> what is the diff between static int and int ...
> Static variables are not a good habit to get into using. My view has
> always been that statics are essentially a localized global variable and
> therefore the same rules apply to them that apply to globals (i.e. keep
> them both to a bare minimum).

IMHO the problem with global variables is that any function that is in
the same scope can modify or access them, and that is the biggest
logical difference between the two. Statics can be very useful if you
need a variable to hold its value between the function calls, but when
that value is not used anywhere else at all - yes, you could use a
member variable (if it's in a class), or a variable in the calling
function that you pass by reference, but if you use a static then you
see the definition when you're looking at the function, and you can be
certain that the variable is not modified or accessed anywhere else,
while with globals you never know what can happen to them (even during
the function call).

> Both statics and globals become
> problematic when you start writing multithreaded applications (e.g. two
> threads accessing the same variable at the same time).

Yes, but with statics that you define in function scope you can be
sure that they are only accessed by that function, and you can avoid
multithreaded problems by simple locks/mutexes.

-- 
Tamas Marki

Reply via email to