469,578 Members | 1,742 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

(guaranteed?) default value of an unresolved external

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.

Nov 15 '05 #1
4 1718
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?

Nov 15 '05 #2


Alfonso Morra wrote:
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.

--
Er*********@sun.com

Nov 15 '05 #3


Eric Sosman wrote:

Alfonso Morra wrote:
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.


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

Nov 15 '05 #4
Alfonso Morra wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rodolphe | last post: by
1 post views Thread by Aravind | last post: by
5 posts views Thread by cschettle | last post: by
6 posts views Thread by sadegh | last post: by
reply views Thread by Ryan Gaffuri | last post: by
2 posts views Thread by =?Utf-8?B?YmFzaA==?= | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.