Thread: True global variables?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    True global variables?

    I basically have a couple C files and a couple header files. In one header file, I have the declaration of a struct... Following that is a declaration of a variable of that struct type

    Example (myHeader.h):

    Code:
    struct myStruct
    {
        int a;
        int b;
        char c;
    };
    
    struct myStruct data1;
    My question is, how do I make it so that when a .c file "includes" myHeader.h, it shares the same data1 with any other C file that included it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Declare data1 as extern in the header file, then define it in one of your source files.

    You might want to choose a much better name than data1, of course, given the pitfalls of global variables.
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by laserlight View Post
    Declare data1 as extern in the header file, then define it in one of your source files.

    You might want to choose a much better name than data1, of course, given the pitfalls of global variables.
    Whats the difference between doing that OR in my header doing static instead of extern? If I do static, then I don't have to declare it in my C file... Whats the difference?

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    static would make it a different instance for all 'c' files that include it
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    extern basically allows a source file access to another source file's global. static doesn't do that, it includes a new instance in each source file.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Why would I have to declare a variable again in a C file when I already made it extern in a header file? I don't understand that part... if a file includes the header already, shouldn't it be global to whoever includes it? Do I need to keep declaring that variable in every C file to share it? Remember, I only want one instance throughout all C files.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Extern just means "this variable exists somewhere else". If you don't actually create it at some point, everybody just ends up playing hot potato and the variable can't be found (since it doesn't, you know, exist).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why would I have to declare a variable again in a C file when I already made it extern in a header file?
    That "extra" declaration is the definition of the variable, without which all you have is a variable name without storage.

    Do I need to keep declaring that variable in every C file to share it? Remember, I only want one instance throughout all C files.
    Yes, but again extern should be used to get what you want.
    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

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the variable has to exist in actual source code. Which means its compiled into one of your ".o" files. When you put it in a header, you are copying it into whatever source files use the header. You can only do this once or it will mean the same variable is declared again and linking will not work.

    so extern is declaring, but not defining the variable. It's just telling others about this variable that exists in some source file.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    My last post was convoluted. Easy description: extern doesn't define the variable. It just tells others about it.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  3. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM
  4. General global bool variables?
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 02-27-2005, 09:16 PM
  5. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM