Re: multiple definitions....



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

lbj137@xxxxxxxxx wrote:
> I have two files: A.c and B.c. In both files I define a global
> variable, int xxxx; When I compile with a green hills compiler (and
> also i think with a GNU compiler) I get no errors or warnings. Only
> when I initialize xxxx to two different values in A.c and B.c
> respectively do I get a 'multiple definitions' error. I was under the
> impression that I would get such an error even without initializing
> xxxx twice, simply because i am (ostensibly) allocating memory for it
> twice. what is going on here?

While this is not strictly a C issue (at least I don't think it is), and thus
is off topic here, I think that a short explanation might help.

Compilers can be smart. If they see that a global is allocated but not
initialized, they can just make space for it in their map of where data is
stored, but not actually generate the instructions/data to fill in the space.

When a global is allocated by more than one translation unit, the linker (or
what ever performs that task) can attempt to resolve the 'space left for the
global' into a usable (load image, execution image, 'core' ) form.

When both tu initialize the global, the linker can't decide which
initialization to use, and you get a multiple definition error.

When only one tu initializes the global, the linker uses that tu's allocation,
and fills in the blanks into the tu that didn't initialize the global.

When neither tu initialize the global, the linker allocates it directly, and
fills in the blanks in both of the tu's.

In other words, you can allocate a global in multpile translation units, but
you are only permitted to give it an initial value in one of those TUs.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCshw0agVFX4UWr64RAtdeAJ9GRAARlW4hIHJmCr3JBWVmacGRFACbBPVp
dJ46PHXfhoXTfNx4KEjSXmM=
=z5/3
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: abstract sub programs overriding
    ... >>compiler can determine that Object is never referenced before it is ... >>implicit Finalize. ... >>the Initialize is user defined. ... > if that initial value is an aggregate, it has to be built directly in the ...
    (comp.lang.ada)
  • Re: compiler bugs
    ... In a language like C, the behavior of programs that reference ... Not if the unoptimizing compiler initializes all variables to some ... portion of the array in your algorithhm. ... that one actually does not need to initialize all variables (unlike ...
    (comp.compilers)
  • Re: Javac issues "might be uninitialized" for null test
    ... And any compiler worth its salt would ... Java does not initialize variables (as opposed to class ... Some languages "initialize with garbage" and allow ... compare the non-existent value to `null' is erroneous. ...
    (comp.lang.java.programmer)
  • Re: GC in Jons raytracing benchmark
    ... >>> so a sufficently smart compiler could optimize them away. ... are more lisp temporaries, and/or they're bigger. ... > slow at allocating and immediately collecting millions of temporary ... > fix for the CL code. ...
    (comp.lang.lisp)
  • Re: Replicating results
    ... gradient calculation used in a large optimization routine. ... It will make sure that if you do something like declare a variable called "x1" and use a variable called "xl" the compiler will tell you that xl was undeclared. ... Make sure that you initialize all of your variables, ...
    (comp.lang.fortran)