C++ Conundrum?

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
 
V

Victor Bazarov

Duncan said:
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.

They may not 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?

No compiler should produce garbage. It's either 3 or 0. Both 'a' and
'b' have static storage, which should be zero-initialised before any
dynamic initialisation (the order of which is unspecified) takes place.

So, you have two possibilities, scenario A and scenario B.

Scenario A: objects in TU 'A' are initialised first.
-- static initialisation:
a := 0
b := 0
--- dynamic initialisation
a := 3 // by calling 'f'
b := 3 // by assigning from external 'a'
---- main is called
Output: 3

Scenario B: objects in TU 'B' are initialised first.
-- static initialisation:
a := 0
b := 0
--- dynamic initialisation
b := 0 // by assigning from external 'a' (which is 0 now)
a := 3 // by calling 'f'
---- main is called
Output: 0

V
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top