Thread: Use global variables or pointers?

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Use global variables or pointers?

    I have been reading up in my C++ books and all of them say that using global variables is not very 'safe' and a few books say its down right dangerous. Im curious if it is considered good programming practice to use pointers in place of global variables. Also, is it a bad idea to use so many pointers?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if the pointers are global, then they are "bad" like other global variables. In c++ the best solution is to put global variables into a class so that the class scope will limit access to them. One of the reason globals are considered bad is because they can be easily overridden by local variables with the same name. Put them into a c++ class will probably eliminate that confusion. You will appreciate this more after spending all night searching for a bug just to find out that you used a local variable with the same name as a global, and couldn't figure out why the value of the global variable didn't change!

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    I don't see how using global variables is unsafe.
    I do see how it's bad programming practice though. Having stuff available from all over your program doesn't sound very practical and has a tendency of making your source code into an intangible mess where everything's accessed from everywhere.
    Once a bug gets into code like that, it'll become VERY hard to figure out what's causing the problem.

    (edit) Also, what Ancient Dragon said.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    [preach]
    Pointer. The more pointers the better.

    Never EVER use global variables. Maybe once in your life it will be okay. But other than that, do NOT use global variables.
    [/preach]

    So yeah, the great thing about global variables is that you can use them through all your code, change them where you like, and not have to worry about passing values back and forth. The downfall of global variables is that they can be changed anywhere by anything, since there's no restriction on access to the variable. When anything can access your variable, that's when problems can start to happen.

    Pointers, if you can manage to get your brain around them (it usually takes a while before people get comfortable with them) are fantastic.

    Imagine having a big structure filled with people's names, phone numbers, addresses, etc. And you have one million of these. Without pointers, your program is required to copy ALL the data over into a new variable, i.e.

    Code:
    int AddOne(int Value)
    {
    return Value + 1;
    }
    
    int main(void)
    {
    int X = 0;
    
    X = AddOne(X);
    
    return 0;
    }
    Sorry, no access to a TAB to make the code pretty

    But in that example, when AddOne() is called, the contents of X must be COPIED over to Value, and then when the return happens, the contents of Value+1 are COPIED back over to X. Now, with one int, it isn't that big of a deal. But get millions of big structures, that take up alot of memory to begin with, you can find yourself in trouble easily.

    However, with a pointer, all you'll do is pass the value by reference. This means, that instead of COPYING the contents of the variable, you're only copying over the value of the Address of the variable. It's much faster/efficient to copy only an address and say, "Hey, look over here, here's the data you want, change it however you see fit", than spend the time dumping variable after variable into whatever empty memory you can find.

    So, there's my two cents. I'm sure you'll have close to a dollar by the time everyone's through with this. But hope it helps clear a couple of things up.
    Last edited by Epo; 09-22-2005 at 03:56 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I say don't use globals or pointers in C++.

    As much as possible create your variables on the stack and pass them by reference to functions that need to modify them. Encapsulate related data into classes and provide an interface to read and change that data. Any data that you need for your entire program can be created in main and passed around as necessary.

    There are exceptions, but they are rarer than you think.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I have been reading up in my C++ books and all of them say that using global variables is not very 'safe' and a few books say its down right dangerous.
    Just about all those books use std::cout and std::cin, which are both global variables. So, I think it all comes down to how use them. For std::cout's simple uses, the state isn't messed around with unexpectedly. You won't typically run into problems of function A modifying std::cout state and then function B expecting the other state.

    Im curious if it is considered good programming practice to use pointers in place of global variables. Also, is it a bad idea to use so many pointers?
    Finding an appropriate owner for some classes can often be difficult. Sometimes I end up in situations where without globals and singletons, subsystems that use other subsystems would have access to classses that logically they shouldn't have. Often, too, you have to work around a library's framework.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  3. Global Variables in C++?
    By DeX in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2005, 08:43 AM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. GLobal variables
    By fuh in forum C++ Programming
    Replies: 21
    Last Post: 01-01-2003, 03:11 AM