Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C global variable initialization

Status
Not open for further replies.

scottgai

Programmer
Dec 12, 2001
11
US
Hi Gurus,

I got confused about a C global variable question.

There are 3 very simple C files:

mya.c
---------------------
#include <stdio.h>

int tv=10;
printav()
{
printf(&quot;&av= %x\tav = %d\n&quot;, &tv, tv);
}
------------------------

myb.c
------------------------
#include <stdio.h>

int tv= 20;

printbv()
{
printf(&quot;&bv= %x\tbv = %d\n&quot;, &tv, tv);
}
---------------------------------

mymain.c
---------------------------------
#include <stdio.h>

main()
{
printav();
printbv();
return;
}
-------------------------------------

Build program with following command:

gcc mymain.c mya.c myb.c -o myab

Then the linker complain:

ld: fatal: symbol `tv' is multiply defined:
(file mya.o and file myb.o);

if I change mya.c to :
-------------------------
#include <stdio.h>

int tv;
printav()
{
tv = 10;
printf(&quot;&av= %x\tav = %d\n&quot;, &tv, tv);
}
--------------------------

Then build myab will be successful. result is:

&av= 21784 av = 10
&bv= 21784 bv = 10

Who can help to explain this?
How does C allocate and initialize global variable?

Thanks in advance!!!

Scott
 
May be it is not thing of compiler, rather of linker in Unix. On msvc your example does not work - multiple definition error is output in both cases.
Perhaps &quot;int tv;&quot; does not allocate memory immediately, it works like a &quot;extern int tv;&quot;, the allocation will be done only if there are no one explicit memory allocation with value initialisation like &quot;int tv=10;&quot;?
 
Much thanks for those reponses!
I know using extern could eliminate the error. But what I really want to know is: in what way the linker treat the same global variables defined in two different files.
I also did the following test:

1) using Unix gcc

mya.c myb.c error occurs?
-----------------------------------------------------
int tv; int tv; No
int tv=10; int tv; No
int tv=10; int tv=10; Yes
int tv=10; int tv=20; Yes

2) using Microsoft VC++ 6.0

mya.c myb.c error occurs?
-----------------------------------------------------
int tv; int tv; No
int tv=10; int tv; No
int tv=10; int tv=10; No
int tv=10; int tv=20; Yes

I know it's the problem of linker. However I don't know how does the linker treat the definition of same global variables in two different files.
 
In UNIX at least, all variables declared outside of a function (globally scoped within a translation unit) are placed in a zero initialized segment of memory. If you declare one with the same identifier in more than 1 file and link them together, you essentially are getting a namespace collision.

If you make it explicitly static, it can't be accessed outside the file. If you don't, other translation units (usually source files) can simply declare it as &quot;extern&quot; and do what they will to it's value -- it you don't like it, make it const (readable in other files) or static (unlinkable outside). A good example is how &quot;errno&quot; is declared in errno.h and used elsewhere.

Automatic variables are placed in the block symbol segment, or BSS. These values are not initialized, that is, anything declared within braces of any kind.

In other words, there is nothing &quot;wrong&quot; with GCC in this respect -- it's acting according to Hoyle.
 
milenko

Thanks for the explanation. But I still don't understand why the linker complain when I define variable as following:

mya.c myb.c error occurs?
-----------------------------------------------------
int tv; int tv; No
int tv=10; int tv; No

and it don't when

mya.c myb.c error occurs?
-----------------------------------------------------
int tv=10; int tv=10; Yes
int tv=10; int tv=20; Yes

Could you explain it a little more?
 
>
> 2) using Microsoft VC++ 6.0
>
> mya.c myb.c error occurs?
> -----------------------------------------------------
> int tv; int tv; No
> int tv=10; int tv; No
> int tv=10; int tv=10; No
> int tv=10; int tv=20; Yes
>

Are you sure? When I tested these examples under MSVC 6.0, all of them produced an error.

Your problem actually is an example, what the object programming stands for - use classes instead of plain variables. Or may be the same effect is also with uninitialized class objects? Let try to use some &quot;my_class tv;&quot; in both files mya.cpp and myb.cpp instead of &quot;int tv;&quot;, where my_class is simple class without any constructor:
class my_class
{
public:
int my_property;
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top