Re: Help wanted on some source codes



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?

No, both variable x have external linkage therefore the behaviour is undefined and on some systems, with some options, won't even link. If it does link the compiler is allowed to put both items in the same location, make that location the size of the smaller (probably int) and send a letter to you mother telling her you are dead. Alternatively the compiler/linker could place the two variables in separate locations and send a letter to the accounts department of the company you work for telling them to stop your salary. Anything it does is valid.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
.




Relevant Pages

  • Re: fields for methods?
    ... but only by a compiler that is allowed to ... struct A {void foo();}; ... int static_instance i = 0; ... Is not possible because foo is the only member of A... ...
    (comp.programming)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... This is non-standard (Conceivably your compiler allows it ... tmp1 might not be correctly aligned for u32. ... void change_endian{ ... typedef unsigned int u32; ...
    (comp.lang.c)