Discussion Forum

Global Variable.

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Ajit Pachore
Posted
14-Aug-2004 16:44 GMT
Toolset
C51
New! Global Variable.
In my project I have two files. as follows

File A
int x;
main()
{
x=2;
}

File B

extern int x;
{
x=4;
}

When I compile both files independatly, there is no error. When I link both modules, there is no error. But when I execute this program, I am unable to assign x = 4. Always the value of x remains 2. But in file A I can assign any value to x. Why I can not assign any value to x in File B? I am trying this with Keil compiler.
Read-Only
Author
Dan Henry
Posted
14-Aug-2004 17:00 GMT
Toolset
C51
New! RE: Global Variable.
File B has no function to execute. File A's main() simple assigns the value 2 to 'x' and exits. Try:

File A
extern void change_x(void);

int x;

main()
{
    x = 2;
    change_x();
}
File B
extern int x;

void change_x(void)
{
    x = 4;
}
Read-Only
Author
Ajit Pachore
Posted
14-Aug-2004 17:09 GMT
Toolset
C51
New! RE: Global Variable.
what i mean by my question is

File A

int x;
main()
{
x= 2;
change_x();
}

File B
extern int x;
change_x()
{
x=4;
}

The expected answer is 4. But I am getting 2.
Read-Only
Author
Dan Henry
Posted
14-Aug-2004 17:38 GMT
Toolset
C51
New! RE: Global Variable.
"The expected answer is 4. But I am getting 2."

And I am getting 4. What are you using for a debugger? Try using uVision's simulator, add 'x' to a watch window and step through the program.
Read-Only
Author
Hans-Bernhard Broeker
Posted
18-Aug-2004 09:21 GMT
Toolset
C51
New! RE: Global Variable.
Actually there can be no "expected answer" from your displayed code at all, because it doesn't output anything, anywhere. For what it's worth, the linker would be in its full right to just optimize 'x' away, since you never use the value for anything.
Read-Only
Author
Andrew Neil
Posted
18-Aug-2004 09:24 GMT
Toolset
C51
New! RE: Global Variable.
"the linker would be in its full right to just optimize 'x' away"

To be precise, the Compiler would perform this optimisation...
Read-Only
Author
Hans-Bernhard Broeker
Posted
18-Aug-2004 10:08 GMT
Toolset
C51
New! RE: Global Variable.
the Compiler would perform this optimisation

Not on a global variable like this it wouldn't. Or if it did, that'd be a bug.

The compiler simply doesn't have the information needed to justify eliminating any object of external linkage --- it doesn't know what other translation units might be doing with it.
Read-Only
Author
Andrew Neil
Posted
18-Aug-2004 10:20 GMT
Toolset
C51
New! RE: Global Variable.
"Not on a global variable like this it wouldn't."

Oh yeah...!

Obviously the scope of my reply was too limited (didn't read the whole thread).
Sorry.
Read-Only
Author
Keil Support, Intl.
Posted
25-Aug-2004 13:48 GMT
Toolset
C51
New! RE: Global Variable.
The end of your program is undefined. You should have a while (1) loop in the 'main' function to avoid that your program jumps to nowhere.

Next Thread | Thread List | Previous Thread Start a Thread | Settings