Re: Help wanted on some source codes
- From: Skarmander <invalid@xxxxxxxxxxxxxx>
- Date: Wed, 30 Nov 2005 04:42:09 +0100
slebetman@xxxxxxxxx wrote:
Old Wolf wrote:Becker wrote:
1.=========================================== /* a.c */ int x; int y;
void main() { f(); printf("%x %x\n", x, y); }
/* b.c */ double x;
void f() { x = -0.0; }All of your programs have undefined behaviour because there are two variables called 'x' with external linkage.
The programs are rubbish of course. But wouldn't the file scope of x mean that function f() is actually refering to the double x instead of the int x?
Nope, because technically, there's no difference between the two.
First of all, this program is in violation of:
6.9-5 "If an identifier declared with external linkage is used in an expression [..], somewhere in the entire program there shall be exactly one external definition for the identifier; [..]"
But suppose one of these was an "extern" declaration instead, so there was only one definition. Then we get:
6.2.2-2 "In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function."
So the "int x" and "double x" must be the same object. But of course that's not possible, and we are violating:
6.2.7-2 "All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined."
Needless to say, int and double are not compatible types.
Now, in *practice*, a compiler will indeed treat the 'x' in a.c and the 'x' in b.c as different objects within their respective scopes, compile the units as such, and then the linker will either notice a conflict (best case), create two separate objects (suboptimal but not completely awful case), or not notice anything at all and happily merge the storage for these incompatible objects (worst but unfortunately also most likely case).
Try this: assign some nontrivial value to 'x' in 'f', try to compile and link the program, then run it if this works. You should see the value of the double (partially) reinterpreted as an int. Say hi to the nasal demons for me when you do this.
This notwithstanding, a platform would actually be allowed to format your harddisk immediately after parsing both a.c and b.c, without paying any consideration to scope. The behavior is undefined; scope is irrelevant.
S. .
- References:
- Help wanted on some source codes
- From: Becker
- Re: Help wanted on some source codes
- From: Old Wolf
- Re: Help wanted on some source codes
- From: slebetman@xxxxxxxxx
- Help wanted on some source codes
- Prev by Date: Re: Help wanted on some source codes
- Next by Date: Re: Help wanted on some source codes
- Previous by thread: Re: Help wanted on some source codes
- Next by thread: Re: Help wanted on some source codes
- Index(es):
Relevant Pages
|