C: Global Variables versus Parameters

Sage
Posts: 1,478
Joined: 2002.09
Post: #7
To expand on what DoG said. It's about being able to debug code too.

Again with the example of the variable that should never be -1. It's easy enough to say that, but hard to enforce as DoG said. Now, lets say your program is crashing because the value is -1:

As a global variable, somewhere in your program you are writing myGloblal = (a - b)/c into the variable. Not exactly obvious that it could actually be -1 given the right a, b, and c. Then at some other point in the program you are reading the value back and crashing because you are trying to get the -1 item out of an array or something like that. Now you have to find all the places that can write into that variable and figure out which of them is writing -1 into it.

If it was passed as a parameter to a function when the program crashed, you would be able to open the program up in your debugger and see which function passed -1 to it and where it got it -1 from. This can make it much easier to debug your programs.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 


Messages In This Thread
C: Global Variables versus Parameters - DoG - Jan 13, 2010, 05:03 AM
C: Global Variables versus Parameters - Skorche - Jan 13, 2010 08:19 AM
Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Accessing an inherited class's variables Tobs_ 22 8,369 Feb 28, 2007 05:26 PM
Last Post: mac_girl
  Problems with variables in Obj-C vnvrymdreglage 16 5,918 Oct 2, 2006 10:19 PM
Last Post: vnvrymdreglage
  Should global variables be pointers or full objects? ia3n_g 1 2,066 Aug 4, 2006 05:53 PM
Last Post: OneSadCookie
  where do global variables fall into the memory type? WhatMeWorry 3 2,522 Jun 5, 2006 02:45 PM
Last Post: OneSadCookie
  Arrays or variables containing executable functions Jones 4 3,729 Jun 2, 2006 08:35 AM
Last Post: Zekaric