C++ Conundrum?

Discussion in 'C++' started by Duncan Smith, Nov 13, 2007.

  1. Duncan Smith

    Duncan Smith Guest

    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
     
    Duncan Smith, Nov 13, 2007
    #1
    1. Advertising

  2. Duncan Smith wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 13, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Owen

    Clunky Cache Code Conundrum?

    Jim Owen, Jul 3, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    388
    dave wanta
    Jul 3, 2003
  2. Iain
    Replies:
    0
    Views:
    368
  3. Nobody
    Replies:
    6
    Views:
    667
    Hemal Pandya
    Jul 14, 2005
  4. VisionSet

    RMI conundrum

    VisionSet, Dec 23, 2005, in forum: Java
    Replies:
    2
    Views:
    383
    VisionSet
    Jan 2, 2006
  5. VisionSet

    Repost: RMI Conundrum

    VisionSet, Dec 30, 2005, in forum: Java
    Replies:
    4
    Views:
    383
    John C. Bollinger
    Dec 31, 2005
Loading...

Share This Page