Original Post
What signifies or usually causes you tocreate a global variable in a program?
bad code
While I agree, in principle (and it is easy to be theoritical)...
In actual coding practice however, there are many situations where the most elegant and readable solution is probably using a global variable or object. Sometimes having to pass around a lot of data in functions belonging to different classes it just makes more sense to store certain program-wide information not belonging to any particular class (such situations do arise!) in globals.
but that comes at a huge cost of completely random behavior, which is far far worse than readability problems.
[quote name='way2lazy2care' timestamp='1319476174' post='4876359']
but that comes at a huge cost of completely random behavior, which is far far worse than readability problems.
By 'completely random behavior', I'm guessing you mean static initialization issues in C++?
Even in that environment, you only run into issues if you depend on order of static initialization (which is dicey at best). I don't want to be the global variable advocate; they have many downsides, but randomness isn't something inherent to their nature.
Static initialization is no problem, by the way. The behaviour is well defined if you just only create a single global struct containing any other global objects (or pointers to them) you might need. The order of initialization of a single object is well-defined, and a simple struct wrapper is completely optimized out by the compiler.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more