Are global variables bad?


Question

In C/C++, are global variables as bad as my professor thinks they are?

1
224
1/27/2009 6:36:25 PM

The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

To understand how the application works, you pretty much have to take into account every function which modifies the global state. That can be done, but as the application grows it will get harder to the point of being virtually impossible (or at least a complete waste of time).

If you don't rely on global variables, you can pass state around between different functions as needed. That way you stand a much better chance of understanding what each function does, as you don't need to take the global state into account.

237
2/24/2010 12:07:57 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon