Thread: Global variables keep resetting

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    38

    Global variables keep resetting

    I have 4 global variables defined in my starter module of a C dll, like so:

    Code:
    LFLOAT GTEMPX;
    LFLOAT GTEMPY;
    LFLOAT GTEMPSIZE;
    long GPYRAMID;
    
    long __stdcall ExtractRas(LFLOAT *tempX, LFLOAT *tempY, LFLOAT *tempSize, long *tempPyramid) {
    
    rest of code etc................
    later on in the code I set these globals to have some values set to them (from the incoming parameters ExtractRas, VB code to be exact). It simply does this:

    Code:
    LFLOAT GTEMPX = *tempX;
    LFLOAT GTEMPY = *tempY;
    LFLOAT GTEMPSIZE = *tempSize;
    LONG GPYRAMID = *tempPyramid;

    in a seperate module I have referred to these globals as:

    Code:
    extern LFLOAT GTEMPX;
    extern LFLOAT GTEMPY;
    extern LFLOAT GTEMPSIZE;
    extern long GPYRAMID;
    
    LONG get_tile_envelope (SE_RASTERATTR rasterattr, TILEENV* tileenv) 
    {
    rest of code etc........................
    Ok - so I think I have used the correct syntax, and everything is in the right place where it should be. So when I debug this dll, I see that the values in the globals (which are various numbers set from the external VB code), suddenly reset to 0.000000000000, as soon as "get_tile_envelope" function is called!!!!

    This is not good, I am so close to finishing, and have tried many things, does anyone know why this type of thing is happening?

    Many thanks

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    A variable in C can be declared in the following manner:
    Code:
    TYPE name = value;
    If you look at your code, you will notice it is following this pattern. So you are declaring new variables rather than assigning to existing variables!
    Code:
    LFLOAT GTEMPX = *tempX;
    LFLOAT GTEMPY = *tempY;
    LFLOAT GTEMPSIZE = *tempSize;
    LONG GPYRAMID = *tempPyramid;
    This syntax is declaring local variables and assigning values to them. The global variables are not touched. Variables declared at different scopes can have the same name, in which case the most local variable is used.

    If you do not wish to declare local variables and just want to assign values to the global variables simply omit the type.
    Code:
    GTEMPX = *tempX;
    GTEMPY = *tempY;
    GTEMPSIZE = *tempSize;
    GPYRAMID = *tempPyramid;
    I'm not exactly sure what a LFLOAT is. If it is an alias for double, you should consider using double instead as it is more familiar to most programmers. Also, most programmers do not use all uppercase for variable names, except constants.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    If you do not wish to declare local variables and just want to assign values to the global variables simply omit the type.
    I omitted the type to stop defining them as local and hopefully assign values to globals, however, I got a staggering 103 errors and 26 warnings. I wont post them as there are too many.

    Its as if its not picking up the fact that their are globals in existance!

    Is it the fact that I am creating a dll and you cant have globals.

    Is it the fact that I dont have a "main", perse? because its a dll?

    Or could it be that I am referencing a lot of "External Dependancies" which define special types and structs and external functions etc. Also define LFLOAT as double! (just so you know). Should the globals be referenced here? or something.

    Just dont know what to do

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're using a type which is defined some place, you have to at least have a way to notify the compiler of that definition. Usually this means including a header file which has defined the object in question. IE:
    Code:
    /* something.c */
    
    extern struct foo x;
    
    void bar( void )
    {
        x.somevar = 10;
    }
    For the above to be valid, we have to actually have some point of reference to the actual definition of 'struct foo'. Otherwise, we don't know that there even is a 'somevar' in it. So, this typically would be done with something like:
    Code:
    /* foo.h */
    
    struct foo
    {
        int somevar;
    };
    And then...
    Code:
    /* something.c */
    #include "foo.h"
    
    extern struct foo x;
    
    void bar( void )
    {
        x.somevar = 10;
    }
    Now here we can access elements of foo. However, since this is actually an extern variable, it's really referencing a variable 'x' in yet another file. So there actually has to be a globa variable some place else, to which we can actually use the external reference.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> I omitted the type to stop defining them as local and hopefully assign values to globals, however, I got a staggering 103 errors and 26 warnings. I wont post them as there are too many. <<

    Usually, when you suddenly get a lot of errors, it is because the first few are cascading and causing the rest. Therefore, it can be very useful to post the first three or four.

    >> Is it the fact that I am creating a dll and you cant have globals. <<
    >> Is it the fact that I dont have a "main", perse? because its a dll? <<

    No. DLLs can have globals and the lack of a main function shouldn't be a problem.

    Without the code or the error strings, I can only guess, but I think the problem may be that by changing some of your declarations to statements. you now have statements above your variable declarations. I have previously discussed this here.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Ok here are the first 10 or so errors:

    See if you can make sense of them.

    Code:
    Compiling...
    ExtractRas.c
    c:\cprogs\extractras\extractras.c(71) : error C2143: syntax error : missing ';' before '<class-head>'
    c:\cprogs\extractras\extractras.c(73) : error C2143: syntax error : missing ';' before 'type'
    c:\cprogs\extractras\extractras.c(76) : error C2143: syntax error : missing '{' before '.'
    c:\cprogs\extractras\extractras.c(76) : error C2059: syntax error : '.'
    c:\cprogs\extractras\extractras.c(77) : error C2059: syntax error : 'if'
    c:\cprogs\extractras\extractras.c(79) : error C2059: syntax error : 'else'
    c:\cprogs\extractras\extractras.c(86) : error C2099: initializer is not a constant
    c:\cprogs\extractras\extractras.c(87) : error C2059: syntax error : ','
    c:\cprogs\extractras\extractras.c(87) : error C2143: syntax error : missing ')' before 'string'
    c:\cprogs\extractras\extractras.c(87) : error C2143: syntax error : missing '{' before 'string'
    c:\cprogs\extractras\extractras.c(87) : error C2059: syntax error : '<Unknown>'
    c:\cprogs\extractras\extractras.c(87) : error C2059: syntax error : ')'
    c:\cprogs\extractras\extractras.c(93) : error C2059: syntax error : ','
    c:\cprogs\extractras\extractras.c(93) : error C2143: syntax error : missing ')' before 'string'
    c:\cprogs\extractras\extractras.c(93) : error C2143: syntax error : missing '{' before 'string'
    c:\cprogs\extractras\extractras.c(93) : error C2059: syntax error : '<Unknown>'
    c:\cprogs\extractras\extractras.c(93) : error C2059: syntax error : ')'
    Thanks - Simon

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Actually anonytmouse - your comments about having variables being above statements was the clincher - I moved it well down the code and everything compiles just fine!!! Many thanks - and I feel really stupid now it was so simple!

    I thought that by referencing globals and the paramaters straight after they were referenced would not cause any problems - because they have been delcared - but apparently not so. Anyway most important thing is - it works, and you helped me a lot - many thanks, Simon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variables.
    By vapanchamukhi in forum C Programming
    Replies: 5
    Last Post: 09-15-2008, 05:02 AM
  2. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10: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