d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Static Global Variables Bad Practice?
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 24 2012 03:40pm
Asked my professor today and he seemed to say that Global variables in general are bad.

I already kinda knew this was the case, but he didn't really say much about static global variables.

My question: Can you make a static global variable, if so, does it have the same pitfalls as a regular global variable?

ie: one that only works for the said file?

My example:

Working on a project that sorts latin words in a tree alphabetically. If the same occurrence of a word happens, instead of the add() function just returning and doing nothing, we need to add +1 to some integer counter.

I wanted to add a line of code in my add() function, so that in the case of return, it would be like

Code
include <xxx>
// decs located in a .h file

int counter = 0;

int main {....

// this is the main .cpp file

____________________________________________________________

// this is a function definition

void add(string someString, .....)
{
...
if (word == somestring)
{
counter++;
return;
}
....


I wanted to do something like that. But he told me it would just be best to pass by reference. I already knew this was the way to go, but I am wondering if a static global

1) exists
2) serves any purpose without huge pitfalls.

This post was edited by Eep on Oct 24 2012 03:44pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 24 2012 03:46pm
defining a variable as static just means that variable will retain its memory address no matter what. its generally used for passing arrays back to the function which called the funciton.

ex:

Code
char *something()
{
  static str[] = " hi";

return str;
}


will create a static memory address that will not be demolished when the function returns meaning the user can use that address as they pleased where as


Code
char *something()
{
  str[] = " hi";

return str;
}


will return the address but your compiler will yell at you until you cry and there is no way to know if that memory address will indeed still be pointing to that string when the function returns. (most of the time it will not and you will either be pointed to garbage or get a segfult.)

This post was edited by AbDuCt on Oct 24 2012 03:46pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 24 2012 03:48pm
Quote (AbDuCt @ Oct 24 2012 04:46pm)
defining a variable as static just means that variable will retain its memory address no matter what. its generally used for passing arrays back to the function which called the funciton.

ex:

Code
char *something()
{
  static str[] = " hi";

return str;
}


will create a static memory address that will not be demolished when the function returns meaning the user can use that address as they pleased where as


Code
char *something()
{
  str[] = " hi";

return str;
}


will return the address but your compiler will yell at you until you cry and there is no way to know if that memory address will indeed still be pointing to that string when the function returns. (most of the time it will not and you will either be pointed to garbage or get a segfult.)


so for my example I am better off just passing by ref then I suppose
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 24 2012 05:31pm
in general you don't want to have any kind of mutable state in a global, because you have no control over who can update it and when... which means that any part of your program that depends on said global/state has unpredictable behavior at any given point in time.

generally when designing programs (or pieces of programs), your program's behavior should depend only on its inputs on and its internal state.

This post was edited by irimi on Oct 24 2012 05:34pm
Member
Posts: 1,756
Joined: Feb 9 2011
Gold: 112.63
Nov 6 2012 09:17pm
Quote (irimi @ Oct 24 2012 06:31pm)
in general you don't want to have any kind of mutable state in a global, because you have no control over who can update it and when... which means that any part of your program that depends on said global/state has unpredictable behavior at any given point in time.

generally when designing programs (or pieces of programs), your program's behavior should depend only on its inputs on and its internal state.



+1 Thumbs up
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 6 2012 09:24pm
Quote (tamubedu @ Nov 6 2012 10:17pm)
+1 Thumbs up


why are you just necroing random threads for +1's.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll