Difference between static variable and global variable in C++?
Difference between static variable and global variable in C++?
a global variable isn't defined inside a class, whereas a static variable is. a global variable dies with its file, whereas a static variable dies when no instance of the class in which it is defined exists.
That's a pretty poor description.
Regarding what they are: the two different types of variables have different visibility scopes. A static variable will only be visible (that is, accessible) inside the function it is defined. Alternatively, if it resides in a class, it acts like a global. Globals exists on a global level; that is, that can be accessed anywhere.
Re lifetime: both types of variables dies first when the program ends.
Well thanks for your replies.
I would like discuss this further..
So static variables in class,are being shared among the different objects for that class.
Static variables in general are being shared among different functions in a program.
Global variables are being shared among different functions and/or files.
Are above statements correct ??
Also, if some variable is required to accessed form different functions in a program, which one is generally preferred, global or static ?
static LOCAL variables are just global variables, but can be accessed only from the function in which they are declared. And there is one important thing: they are initialized not on the program startup, but with the first call of the function. This means - static local variables kill multithreading.Static variables in general are being shared among different functions in a program.
Personally, I never make static (variable) fields, unless they are marked as 'const'. But still, class marked as 'const' might not be thread-safe - it may contain mutable fields for example.
Rethink your design / pass this variable as parameter, etc.Also, if some variable is required to accessed form different functions in a program, which one is generally preferred, global or static ?
But if you really want to do this, make it a static field, so it can be hidden, and you will know what class is using it.
Last edited by kmdv; 09-27-2010 at 11:33 AM.
yes, and they can be visible to the entire program, depending on their access level (public, protected, private)
a static variable in a function is visible only to that function, but retains its value between function calls - it is not a stack variable.Static variables in general are being shared among different functions in a program.
a static variable NOT in a function is visible (by name) only to the translation unit (source file) in which it is defined. it is declared in a similar way to a global variable, but it is local to the translation unit.
a global variable is visible to the entire program, regardess of which translation unit is trying to access it.Global variables are being shared among different functions and/or files.
I'd say it's better practice to have a class with static variables, even if that's the only purpose for the class. global variables are generally frowned upon, although as with any rule, there are exceptions.Also, if some variable is required to accessed form different functions in a program, which one is generally preferred, global or static ?
That's not right. The only difference between a global variable and a static member variable is scope. The name of a global is accessible from everywhere -- the static member is only accessible through its class scope, and thus is subject to protection rules.
In all other respects they behave identically. It certainly does not matter when/whether a member of the class is constructed/destructed.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Memory which is allocated on the stack or the heap can be thought of as dynamic because its allocation depends on the flow of program control.
Memory which is allocated once when the program is loaded and then simply exists throughout the lifetime of the program is static because its allocation doesn't depend on any program control structures, the memory gets allocated when the program is loaded. Static memory is essentially allocated by the program loader and not the program itself.
That's the idea anyway, what the keyword static actually does is tell the language how to scope the static data. For example, a class can have a static variable as a member, but that member would not be bound to any particular object of that class, it exists statically within the program. However, the static member is linguistically restricted to the scope in which it was defined (including public/protected/private access). The same thing happens when you declare a variable within a function as static.
It's all about scope.
edit:
Sorry, some people prefer to call heap memory dynamic and stack memory automatic but when I say dynamic here I basically mean "non-static" and IMO I think it helps make clear what static memory is.
Last edited by BMJ; 09-27-2010 at 04:25 PM.