Thread: Shared Variables Question.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    Shared Variables Question.

    Hi, I am trying to make a program that uses lots of variables. I would like each variable to be accessible to every function used in the program, and also available to programs that use preprocessor directives to include it.

    Just a sample piece of code will do thanks,

    Regards,
    mintsmike

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mintsmike
    Hi, I am trying to make a program that uses lots of variables. I would like each variable to be accessible to every function used in the program, and also available to programs that use preprocessor directives to include it.
    Sounds like a bad idea, i.e., overuse of global variables.

    Perhaps you should state what is the real problem that you are trying to solve.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mintsmike View Post
    Hi, I am trying to make a program that uses lots of variables. I would like each variable to be accessible to every function used in the program, and also available to programs that use preprocessor directives to include it.

    Just a sample piece of code will do thanks,

    Regards,
    mintsmike
    maybe you better consider the career of horror stories writer?

    Job of the programmer is difficult without need to read such a terrible code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    I just want to know how to define a global variable?

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    It's bad programming practice to do that, but if you really want to do it and suffer any consequences thereof, you can 1) make one include file and use it in your program, or 2) you can list each variable type and initialize it above the main() declaration. This makes every variable above the main() as a "global" variable. As I said, it's not good programming practice to do this. For many variables, it is not necessary that they have a "life span" during the entire time the program is open.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't do that! Using global variables is very bad programming style, because sooner or later (usually about the same point as you start having more than one source-file using the same variable) it gets troublesome to figure out which function(s) affect what variable(s).

    Now, having said that, the way you do it, if you have a very good reason to share one or two global variables between different modules is this:
    Code:
    // globals.h
    
    extern int myGlobalVar1;
    extern double myGlobalVar2;
    Code:
    // somefile.c
    #include "global.h"
    
    ...
    code using myGlobalVar1 & 2
    ...
    Code:
    // main.c
    #include "globals.h"
    
    int myGlobalVar1 = 42;
    double myGlobalVar2 = 3.1415926;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mintsmike
    I just want to know how to define a global variable?
    With careful deliberation. Frankly, I am not convinced that the knowledge is safe in your hands, though it is quite easy, and also quite easy to find out by searching this forum and/or the Web.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Thank you for your replies and yes, I have taken on the advice you have given me

    I will now try and limit my use of global variables.

    Regards,
    mintsmike

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    And another one saved from The Dark Side. Good job guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-12-2009, 06:14 PM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  5. question about declaring global variables
    By Dag_ in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 06:03 AM