D
Duncan Smith
I'm pondering one of those job interview style conundrums you see from
time to time, I think I have the answer, but it would be nice to back
it up against a specifcaton or something concrete to be sure...
Here goes:
<code snippet>
/// File A
#include <iostream>
extern int f(void);
extern int b;
int a = f();
int main(int argc, char* argv[])
{
std::cout << b;
return 0;
}
/// File B
extern int a;
int b=a;
int f()
{
return 3;
}
</code snippet>
The problem description is that some compilers will correctly output
'3' whereas others may output garbage. I guess this is because of the
depencies in the external linkage, b=a, a=f.
Perhaps the standard leaves the initialization order upto the compiler/
linker implementation, so if Compiler A does a=f first we'll get '3',
but if another compiler sets b=a first, a will be undefined so the
output from cout will be the un-initialized memory address...
Sound about right?
Many thanks,
Duncan
time to time, I think I have the answer, but it would be nice to back
it up against a specifcaton or something concrete to be sure...
Here goes:
<code snippet>
/// File A
#include <iostream>
extern int f(void);
extern int b;
int a = f();
int main(int argc, char* argv[])
{
std::cout << b;
return 0;
}
/// File B
extern int a;
int b=a;
int f()
{
return 3;
}
</code snippet>
The problem description is that some compilers will correctly output
'3' whereas others may output garbage. I guess this is because of the
depencies in the external linkage, b=a, a=f.
Perhaps the standard leaves the initialization order upto the compiler/
linker implementation, so if Compiler A does a=f first we'll get '3',
but if another compiler sets b=a first, a will be undefined so the
output from cout will be the un-initialized memory address...
Sound about right?
Many thanks,
Duncan