Problem with global variable

This is a discussion on Problem with global variable within the C++ Programming forums, part of the General Programming Boards category; Hi guys I am doing a project in which muti file programming is being used, all program file uses a ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    9

    Problem with global variable

    Hi guys
    I am doing a project in which muti file programming is being used, all program file uses a common header file
    #include "bash.h"
    i am using a global variable which is declared in header file (pointer to array).When i call this variable in .cpp file it gives zero value whereas in header file it is giving correct value.
    I have also used the keyword "extern" but of no use.
    pl help

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The main problem is probably that you have a global variable in the first place.

    I have no idea how you think you are "calling" this variable in the header file.

    Your header file should just say "extern whatever" and your main program should declare it, if you have to do this.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    9

    Problem with global variable

    Hi
    In fact in the header file i have define this variable with keyword extern
    extern char array[10];
    In other cpp file i am just declaring it like
    char array[10];
    at both the places i am declaring it after
    using namespace std; and out of main()
    In one of the cpp file I am defining its value where it is showing its correct value but in other cpp file when i am trying to retrieve it, it is showing zeros
    rgds

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by skumarisation View Post
    at both the places i am declaring it
    And that's a problem. One time. You get to declare the variable one time.

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I have run into few global variables that *didn't* end of being a problem.

    Seriously, if you create your header like this you should be fine:
    Code:
    // my header file
    #ifndef MY_HEADER_FILE_HPP
    #define MY_HEADER_FILE_HPP
    
    int myVariable;
    
    #endif
    Then just include that in your CPPs, they all see it but you don't get the multiple includes...(the guards do the work)..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Quote Originally Posted by tabstop View Post
    And that's a problem. One time. You get to declare the variable one time.
    That's not true. You're thinking of the one definition rule. It is possible to declare something more than once, as long as one and only one of those declarations is a definition.

    In any event, for the original problem, the header file needs to be #include'd in all the .cpp files (where array is defined, and also in all other files that need to use array).
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    9
    Hi
    the problem has been sorted out. Just for others interest, since my reqmt was to share the variable between two cpp files having same header file.I used shared memory(metaphor), Inter process comn (IPC) between these two files.It is working fine. The problem with extern what I visualised was that both cpp were making its own copy of variable and not sharing with each other.Both cpp were having main() in it.May be that's the reason
    Thanks to all
    rgds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-13-2009, 08:49 AM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Class member using a global variable, causing problems.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 11:27 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21