(guaranteed?) default value of an unresolved external

A

Alfonso Morra

Ok, this is a bit of a quick hack. I have a flag set in another file
(the default value of the flag is 0. In certain files, access to the
flag symbol is not available - I want to know if unresolved symbols are
given the value of 0 ?. Is this compiler specific or part of the C standard?

PS: It is too long winded to explain WHY I need to do this - I just need
to know if it will work reliably - i.e. I can count on an unresolved
external symbol(assumed to be an int) will have the value of 0.
 
P

Pramod Subramanyan

What do you mean by access is not available? Is the existence of the
variable unknown to that file or is it in a different process and
access unavailable?

Moreover, won't the compiler throw a fit when you access a non-existent
variable?
 
E

Eric Sosman

Alfonso said:
Ok, this is a bit of a quick hack. I have a flag set in another file
(the default value of the flag is 0. In certain files, access to the
flag symbol is not available - I want to know if unresolved symbols are
given the value of 0 ?. Is this compiler specific or part of the C standard?

PS: It is too long winded to explain WHY I need to do this - I just need
to know if it will work reliably - i.e. I can count on an unresolved
external symbol(assumed to be an int) will have the value of 0.

Let me rephrase your question, to be sure we both
mean the same thing by "unresolved symbol." In file a.c
you have

extern int flag;

Usually, your program is built from both a.c and another
file b.c which contains (at file scope, that is, not inside
any function)

int flag = 0; /* or perhaps some other value */

For some reason you decide to build the program from a.c
alone, without b.c to provide the definition of `flag'.
During the build process, you get a message about `flag'
being "unresolved."

If that's your situation, then no: You cannot count on
any particular value for `flag'. You cannot even be sure
that `flag' exists ("unresolved" means that the linker was
not able to find a `flag' anywhere, so its non-existence is
more probable than not). You cannot even be sure that the
build process will produce an executable program; many linkers
will simply fail if unable to satisfy all the references the
program says it requires, some will produce a program-like
file that is somehow marked "not executable" until and unless
you take special action.

From the standpoint of the C language, there are two cases.
If the program ever refers to `flag' in an expression (other
than as the operand of sizeof), there must be exactly one
definition of `flag' in some translation unit ("source file")
that is part of the program. If the program never refers to
`flag' in any expression, it is permissible for `flag' not to
be defined at all. Anything else is simply not a correct C
program.

Since you're concerned about the value of `flag', you
clearly intend to use `flag' in an expression somewhere (that's
the only way you can inspect or change its value). Therefore
the first case applies: Somewhere, `flag' must be defined and
defined exactly once, or else you don't have a program.
 
F

Flash Gordon

Alfonso said:
Ok, this is a bit of a quick hack. I have a flag set in another file
(the default value of the flag is 0. In certain files, access to the
flag symbol is not available - I want to know if unresolved symbols are
given the value of 0 ?. Is this compiler specific or part of the C
standard?

It is NOT covered by the C standard.
PS: It is too long winded to explain WHY I need to do this - I just need
to know if it will work reliably - i.e. I can count on an unresolved
external symbol(assumed to be an int) will have the value of 0.

No. You can't be certain that the application will run if there are
unresolved symbols. In fact, as far as I am aware, on the systems I use
it either won't link or won't run if there are unresolved symbols.

Any solution to your problem is likely to be highly system specific, so
you will have to ask in a group dedicated to your specific system.
 
A

Alfonso Morra

Eric said:
Let me rephrase your question, to be sure we both
mean the same thing by "unresolved symbol." In file a.c
you have

extern int flag;

Usually, your program is built from both a.c and another
file b.c which contains (at file scope, that is, not inside
any function)

int flag = 0; /* or perhaps some other value */

For some reason you decide to build the program from a.c
alone, without b.c to provide the definition of `flag'.
During the build process, you get a message about `flag'
being "unresolved."

If that's your situation, then no: You cannot count on
any particular value for `flag'. You cannot even be sure
that `flag' exists ("unresolved" means that the linker was
not able to find a `flag' anywhere, so its non-existence is
more probable than not). You cannot even be sure that the
build process will produce an executable program; many linkers
will simply fail if unable to satisfy all the references the
program says it requires, some will produce a program-like
file that is somehow marked "not executable" until and unless
you take special action.

From the standpoint of the C language, there are two cases.
If the program ever refers to `flag' in an expression (other
than as the operand of sizeof), there must be exactly one
definition of `flag' in some translation unit ("source file")
that is part of the program. If the program never refers to
`flag' in any expression, it is permissible for `flag' not to
be defined at all. Anything else is simply not a correct C
program.

Since you're concerned about the value of `flag', you
clearly intend to use `flag' in an expression somewhere (that's
the only way you can inspect or change its value). Therefore
the first case applies: Somewhere, `flag' must be defined and
defined exactly once, or else you don't have a program.

OK - I think this answers my question. (I'll have to thinka bout it a
little more to see if the scenario you described fits my situation). Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top