Thread: Wondering if this would be a dangerous use of globals.

  1. #1
    Registered User
    Join Date
    Jan 2005

    Wondering if this would be a dangerous use of globals.

    Er, quick clarification, but are globals bad because they're sloppy, or do they also cause overhead?

    Anyway, that script interpretor I've been working on. I'd like to have four global variables -- three vectors, and a stack. Keep in mind that these aren't necessarily TRULY global. They might be in a function of some sort. However, they'd be pretty much open, so no matter what I stick them in, they'd probably act open. That, and I'm probably going to be declaring them extern, which... causes some sort of globalization anyway?

    Anyway, roight.

    The vectors are as follows: One for script-created variable names, one for the data for these vars, and a current_thing vector, which just floats around, absorbing data from other vectors for SPECIAL PROCESSING. The stack's just some stack that anything can be stuffed into. Ideally, it'll be empty unless it's in use.

    I don't think the access will be a problem here. Only one or two functions will be able to alter the current_thing vector in ANY way. The other two will be, primarily, having things added TO them. And while it's technically possible to remove stuff from them via scripts, there's no real reason to do this. And even then, the problems that would be caused would be more from improper scripting than the globalness.

    The stack kinda worries me here. I'm afraid something will accidentally get left on it, and this gunk will build up, like creme of tartar on the basin of a wine barrel, and slowly devour the system's chocolatey innards (ed note: I do not have a chocolate computer). Then again, the stack just takes strings, so unless thy're particularly massive strings, or particularly numerous, I think THIS should be okay.

    I'm not sure about the infinite possibility for mutual dependency thing. This is very blatantly a BAD HAPPENING if it occurs. I BELIEVE that the read/write functions should be limited and controlled enough that nothing will break. Current_thing makes me suspicious, though, because I'm not particular sure how that'll get coded and how I'll be picking it apart.

    I guess I'm just wondering, if I do go ahead and go global (or use locals in a global-esque manner) what are some potentially VERY BAD THINGS to look out for? Any tips on managing these things in ways that aren't global, or ways at improving management?

    (* I could probably safely pass references to these things to the functions, although I doubt that'd alleviate the Totally Random Access problems. If anything, I'd fear that this would be MORE sloppy. In this instance, I genuinely believe that, at the least, the stacks and data vectors should stay in one place. Current_room could probably get safely passed around, though).

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Quote Originally Posted by suzakugaiden
    Er, quick clarification, but are globals bad because they're sloppy, or do they also cause overhead?
    [Sorry, I may digest the rest later.]

    No, quite the opposite. Globals are bad because of scope. They are unfortunately very effiicient.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Locals VS Globals (Mem Location)
    By csonx_p in forum Windows Programming
    Replies: 2
    Last Post: 06-25-2008, 02:35 AM
  2. Globals initialization
    By New++ in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2004, 01:11 PM
  3. Why is the gets function dangerous?
    By Kevin.j in forum C Programming
    Replies: 2
    Last Post: 09-27-2002, 05:18 PM
  4. wondering about people here
    By moemen ahmed in forum A Brief History of
    Replies: 23
    Last Post: 07-08-2002, 09:33 PM
  5. the gets() function is dangerous
    By itld in forum Linux Programming
    Replies: 8
    Last Post: 12-27-2001, 07:52 AM