views:

120

answers:

4

Hi,

I am facing following quirky error.

I have a workspace in MSVS2005 of all C code. I have declared a global variable in one C file.(file1.c) This file has function main() in which, I initilaize the value of that variable = 0.In other C file(file2.c). From main there is a function call to a function(func1 in file2.c) which sets the value of this global variable to 1. In file2.c I have declared the global variable as "extern .." and accessed it. But what i noticed is that in the main function moment the code execution enter the function func2, I see in the watch window that the address of that global variable itself is changed to a totally different address(In watch window I am watching &variable). As a result, when the value of that variable is set to 1, it writes the value 1 to altogether different memory address itself. So when later I use this variable to check in a if condition(if variable == 1), it still shows value of 0 and does not go satisfy the if condition and does not take that code path , where it was expected to have taken that path.

Workaround: I declared that variable in one of my exisitng global structure, and then accessed this variable for doing the same operations; the code works as expected.

So what could be the explanation for the error which is causing the address of the global variable to be changed if its declared as a global in some C file? It does not matter in which *.c file i declare it and which file I access it using "extern" , the result is same global variable address change and subsequent errorneous operation.No optimization option is enabled.

Thanks,

-AD

A: 

Maybe try declaring it volatile (not sure if that's even valid for globals) and disable any compiler optimizations in case it's getting tricky somehow.

A: 

If the variable has a different address in different translation units, you are not seeing one but at least two variables with the same name.

Most common cause: You may have accidently declared a local variable on the stack with the same name. Check your code for this. If the variables are really global the linker should complain if two translation units contain the same symbol.

If this does not help, and if you still see multiple copies of the same symbol-name it's probably best to take a look at the map file (can be enabled in the linker-settings).

All external symbols are listed there with their name, address and (most important in your case) the object-file that contained them.

The addresses in the map-file may be just offsets. In this case do all your calculations relative to a symbol that is known to exist only once. the main() entrypoint might be good for this.

Nils Pipenbrinck
A: 

Probably a typo or some thing similar in your code. Try this working demo:

file1.c

int variable;
void fun1(int k);

int main()
{
    printf("%d\n", variable);
    fun1(4);
    printf("%d\n", variable);
}

file2.c

extern int variable;

void fun1(int k)
{
    variable = k;
}

Output:

0
4

To compile:

cl.exe file1.c file2.c
alex2k8
+1  A: 

Can only guess without actually seeing the code, but here are 2 possibilities:

  1. the global variable is being hidden by a local in either main() or func2() (or maybe func1() - the question mentions func1() but I suspect that's a typo - this is why cutting and pasting code is quite important);
  2. you are mistakenly declaring the global variable as static in file1.c and have an initializer on your extern declaration in file2.c. Having an initializer on the extern declaration will cause that declaration to be a definition, too.
Michael Burr