CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    Value of Global variable not consistent

    I have created a program wide global variable in a .h file called globals.h
    Inside the .h file, the global variable is declared as:

    extern float g_fFrameTime;

    2 other classes use this global variable.
    I included globals.h in each of these classes and declared a "float g_fFrameTime;" private data member in these 2 classes.

    The first class assigns a value to g_fFrameTime via
    g_fFrameTime = GetFrameTime();

    while the second class uses the value of g_fFrameTime to perform some calculations.

    However, when I printed out the value of g_fFrameTime of these 2 classes into 2 seperate text files respectively, their values differ. The first class which assigns a value to the global variable prints the correct value to the file (values hovering around 0.01) while the second class which is supposed to use the value of g_fFrameTime prints different values to another file (values around -9.xxxx).

    Besides these 2 classes, no other classes assigns values to or uses the global variable.

    What is the problem causing this inconsistency?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Value of Global variable not consistent

    Quote Originally Posted by ZhiYi
    I included globals.h in each of these classes and declared a "float g_fFrameTime;" private data member in these 2 classes.
    ...
    What is the problem causing this inconsistency?
    A private data member is not a global variable. Effectively, you declared three variables named g_fFrameTime. One is in global scope (but apparently was never defined and used), the other two are in the scope of their respective classes. If you really want to use a global variable, define it in exactly one source file, in global scope.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2005
    Posts
    200

    Re: Value of Global variable not consistent

    No wonder the values are so strange.

    Is declaring an extern float in a .h file and including the header file in the 2 classes the correct method to use that global variable?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Value of Global variable not consistent

    Quote Originally Posted by ZhiYi
    Is declaring an extern float in a .h file and including the header file in the 2 classes the correct method to use that global variable?
    Yes, but as I noted the variable needs to be defined (not declared extern) somewhere in a source file. Also, if it is feasible to avoid the global variable, then you should consider not using one.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Value of Global variable not consistent

    Quote Originally Posted by ZhiYi View Post
    No wonder the values are so strange.

    Is declaring an extern float in a .h file and including the header file in the 2 classes the correct method to use that global variable?
    One and only one source file must declare the global variable without "extern". All other source files must declare the global variable as "extern".

    Also, your description is not clear, and an actual code example from you would explain what you're doing. What do you mean by "private variable"? A variable that is declared outside the scope of any function is either global, or a static variable that is only known to the module that is being compiled. There is no such thing as a "private" global variable.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured