Global variables

This is a discussion on Global variables within the C++ Programming forums, part of the General Programming Boards category; I know it is bad practice to use globals, so please don't lecture me. However, I am using one global ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    48

    Global variables

    I know it is bad practice to use globals, so please don't lecture me. However, I am using one global variable and it is doing something wierd. I have a value input from a file to it, and its value works fine throughout that function. As soon as the program leaves that function (and moves to a different cpp file) the value of the variable is lost and becomes 0. I was wondering if anyone knows the cause of this?
    The flow is such:
    data_driver.cpp calls a function in In_out.cpp
    this function has a line that inputs a number from the file and sets a variable to this number
    In_out.cpp eventually returns, and in data_driver.cpp the value of that variable is now 0

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Maybe you accidentally defined more than one variable with the same name.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In the other file you might need
    Code:
    extern type variable;
    [edit]
    You probably have
    Code:
    type variable;
    and left out the extern. That declares another variable of the same name, which is, of course, undeclared.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    um... why are you including a .cpp file, and why is there a function in it...?

    secondly, don't use global variables. pass the variable to the function using a reference, or have the function return a value, then use it in an assignment statement.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    48
    Ok, sorry, let me clear it up a bit.
    The variable I'm dealing with is declared in a header file. This header file is included in both data_driver.cpp and In_out.cpp.
    data_driver, like its name, is the driver for the program at the moment, In_out.cpp is a file that has the definitions of a few functions (declared in In_out.h which is included in data_driver.cpp)
    So, data_driver calls a function in In_out.cpp. This function opens a file, reads in a number, and sets it to p.nummark (p is the stuct declared in the header file as global with one of its members being nummark)
    p.nummark then has the correct value until it exits the function in In_out.cpp, so when I output p.nummark right after the function call in data_driver.cpp it has a value of 0 instead of the value it had in the function in In_out.cpp

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    where did you instantiate the struct?

    code would be helpful too...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    48
    header.h:
    Code:
    struct paramvar
    {
        int nummark;
        double loc_net_dist;
    };
    
    static paramvar p;
    data_driver.cpp:
    Code:
    #include <iostream>
    #include "In_out.h"
    #include "header.h"
    
    using namespace std;
    
    int main()
    {
        datavar data;
    
        Get_Genetic_Distances(data);
        cout << "nummark is " << p.nummark << endl;
    
        return 0;
    }
    In_out.cpp
    Code:
    #include "header.h"
    //...
    void Get_Genetic_Distances(datavar &data)
    {
    //...
        inFile >> p.nummark;
        cout << "Number of marks is " << p.nummark << endl;
    //...
    }
    I didn't put it all here, but there are no other assignment statements for p.nummark
    this just seems to be the relevant code
    the cout statement in In_out.cpp outputs 'Number of marks is 200'
    the cout stament in data_driver.cpp outputs 'nummark is 0'

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you're getting screwed over by the globalization... what you did was instantiate paramvar in two different places...

    what you need to do is include header.h in data_driver.cpp, and instantiate in main. then rewrite Get_Genetic_Distances to either take a reference to an int as it's argument, or return an int.

    reworked, your files look like this (I'm using references):

    header.h
    Code:
    struct paramvar
    {
        int nummark;
        double loc_net_dist;
    };
    data_driver.cpp
    Code:
    #include <iostream>
    #include "In_out.h"
    #include "header.h"
    
    using namespace std;
    
    int main()
    {
        parmvar p;
        datavar data;
    
        Get_Genetic_Distances(data,p.nummark);
        cout << "nummark is " << p.nummark << endl;
    
        return 0;
    }
    in_out.cpp
    Code:
    //...
    void Get_Genetic_Distances(datavar &data,int&p)
    {
    //...
        inFile >> p;
        cout << "Number of marks is " << p << endl;
    //...
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 03:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 09:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM

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