Function call after switch but before first case - behavior?

Discussion in 'C++' started by yndygo@gmail.com, May 1, 2007.

  1. Guest

    Alright then - I know it's got to be out there somewhere, but I can't
    find it...

    I'm looking at a piece of code and there's some debate as to what the
    behavior will be.

    Given a switch/case where there is a function call after the switch
    but *before* the first case statement, will the function call never
    get reached? or always?

    To whit:

    switch (value) {

    //this is where weird function call is... comment says run every
    time
    randomfunction(x,y);

    case 1:
    cout << "foo!\n";
    break;

    case 2:
    cout << "fah!\n";
    break;

    default:
    cout << "foofah!\n";
    break;
    }

    So what happens? does randomfuntion() get called every time? never?
    The code has been successfully compiled... but no idea if there was a
    warning or not.

    Anyone?
    , May 1, 2007
    #1
    1. Advertising

  2. Marcus Kwok Guest

    wrote:
    > Alright then - I know it's got to be out there somewhere, but I can't
    > find it...
    >
    > I'm looking at a piece of code and there's some debate as to what the
    > behavior will be.
    >
    > Given a switch/case where there is a function call after the switch
    > but *before* the first case statement, will the function call never
    > get reached? or always?


    With VC 7.1, I get no warning, and the statement never gets executed.
    With Comeau Online, I get a "statement is unreachable" warning.

    I'm not sure what the Standard says about it, but I am inclined to agree
    with what Comeau says.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, May 1, 2007
    #2
    1. Advertising

  3. * :
    > Alright then - I know it's got to be out there somewhere, but I can't
    > find it...
    >
    > I'm looking at a piece of code and there's some debate as to what the
    > behavior will be.
    >
    > Given a switch/case where there is a function call after the switch
    > but *before* the first case statement, will the function call never
    > get reached? or always?
    >
    > To whit:
    >
    > switch (value) {
    >
    > //this is where weird function call is... comment says run every time
    > randomfunction(x,y);
    >
    > case 1:
    > cout << "foo!\n";
    > break;
    >
    > case 2:
    > cout << "fah!\n";
    > break;
    >
    > default:
    > cout << "foofah!\n";
    > break;
    > }
    >
    > So what happens? does randomfuntion() get called every time? never?
    > The code has been successfully compiled... but no idea if there was a
    > warning or not.


    A switch statement is essentially a computed goto. Control transfers
    directly from the controlling expression to the corresponding label.
    From this you should be able to figure out what happens.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 1, 2007
    #3
  4. wrote:
    > Alright then - I know it's got to be out there somewhere, but I can't
    > find it...
    >
    > I'm looking at a piece of code and there's some debate as to what the
    > behavior will be.
    >
    > Given a switch/case where there is a function call after the switch
    > but *before* the first case statement, will the function call never
    > get reached? or always?
    >
    > To whit:
    >
    > switch (value) {
    >
    > //this is where weird function call is... comment says run every
    > time
    > randomfunction(x,y);
    >
    > case 1:
    > cout << "foo!\n";
    > break;
    >
    > case 2:
    > cout << "fah!\n";
    > break;
    >
    > default:
    > cout << "foofah!\n";
    > break;
    > }
    >
    > So what happens? does randomfuntion() get called every time? never?


    Never.

    > The code has been successfully compiled... but no idea if there was a
    > warning or not.


    There might be if the compiler understands how to warn of "unreachable
    code".

    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, May 1, 2007
    #4
  5. Guest

    On May 1, 2:41 pm, (Marcus Kwok) wrote:
    > wrote:
    > > Alright then - I know it's got to be out there somewhere, but I can't
    > > find it...

    >
    > > I'm looking at a piece of code and there's some debate as to what the
    > > behavior will be.

    >
    > > Given a switch/case where there is a function call after the switch
    > > but *before* the first case statement, will the function call never
    > > get reached? or always?

    >
    > With VC 7.1, I get no warning, and the statement never gets executed.
    > With Comeau Online, I get a "statement is unreachable" warning.
    >
    > I'm not sure what the Standard says about it, but I am inclined to agree
    > with what Comeau says.
    >
    > --
    > Marcus Kwok
    > Replace 'invalid' with 'net' to reply


    Thanks!
    I got similar results with Visual Studio - but not the Comeau warning.

    I was really wondering if it was compiler specific at that point -
    appreciate the validation and assist!
    , May 2, 2007
    #5
  6. Guest

    On May 1, 2:47 pm, "Alf P. Steinbach" <> wrote:
    > * :
    >
    >
    >
    > > Alright then - I know it's got to be out there somewhere, but I can't
    > > find it...

    >
    > > I'm looking at a piece of code and there's some debate as to what the
    > > behavior will be.

    >
    > > Given a switch/case where there is a function call after the switch
    > > but *before* the first case statement, will the function call never
    > > get reached? or always?

    >
    > > To whit:

    >
    > > switch (value) {

    >
    > > //this is where weird function call is... comment says run every time
    > > randomfunction(x,y);

    >
    > > case 1:
    > > cout << "foo!\n";
    > > break;

    >
    > > case 2:
    > > cout << "fah!\n";
    > > break;

    >
    > > default:
    > > cout << "foofah!\n";
    > > break;
    > > }

    >
    > > So what happens? does randomfuntion() get called every time? never?
    > > The code has been successfully compiled... but no idea if there was a
    > > warning or not.

    >
    > A switch statement is essentially a computed goto. Control transfers
    > directly from the controlling expression to the corresponding label.
    > From this you should be able to figure out what happens.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    Actually, put that way, it makes perfect sense.
    I'd love to see how switch/case works on the machine level, but afraid
    my Assembler is nigh-on non-existant! :p
    , May 2, 2007
    #6
  7. Guest

    On May 1, 2:51 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > Alright then - I know it's got to be out there somewhere, but I can't
    > > find it...

    >
    > > I'm looking at a piece of code and there's some debate as to what the
    > > behavior will be.

    >
    > > Given a switch/case where there is a function call after the switch
    > > but *before* the first case statement, will the function call never
    > > get reached? or always?

    >
    > > To whit:

    >
    > > switch (value) {

    >
    > > //this is where weird function call is... comment says run every
    > > time
    > > randomfunction(x,y);

    >
    > > case 1:
    > > cout << "foo!\n";
    > > break;

    >
    > > case 2:
    > > cout << "fah!\n";
    > > break;

    >
    > > default:
    > > cout << "foofah!\n";
    > > break;
    > > }

    >
    > > So what happens? does randomfuntion() get called every time? never?

    >
    > Never.
    >
    > > The code has been successfully compiled... but no idea if there was a
    > > warning or not.

    >
    > There might be if the compiler understands how to warn of "unreachable
    > code".
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask



    I wasn't clear there - I was reviewing source that came from an
    outside origin - supposedly compiled correctly, and functional, but I
    only have access to the source itself, not the build environment... So
    I don't know if the original compile yielded any warnings to the
    creator - but one would certainly hope it would in this case.

    I'm guessing not tho, as the functionality was flawed.
    , May 2, 2007
    #7
  8. Jim Langston Guest

    <> wrote in message
    news:...
    > On May 1, 2:51 pm, "Victor Bazarov" <> wrote:
    >> wrote:
    >> > Alright then - I know it's got to be out there somewhere, but I can't
    >> > find it...

    >>
    >> > I'm looking at a piece of code and there's some debate as to what the
    >> > behavior will be.

    >>
    >> > Given a switch/case where there is a function call after the switch
    >> > but *before* the first case statement, will the function call never
    >> > get reached? or always?

    >>
    >> > To whit:

    >>
    >> > switch (value) {

    >>
    >> > //this is where weird function call is... comment says run every
    >> > time
    >> > randomfunction(x,y);

    >>
    >> > case 1:
    >> > cout << "foo!\n";
    >> > break;

    >>
    >> > case 2:
    >> > cout << "fah!\n";
    >> > break;

    >>
    >> > default:
    >> > cout << "foofah!\n";
    >> > break;
    >> > }

    >>
    >> > So what happens? does randomfuntion() get called every time? never?

    >>
    >> Never.
    >>
    >> > The code has been successfully compiled... but no idea if there was a
    >> > warning or not.

    >>
    >> There might be if the compiler understands how to warn of "unreachable
    >> code".

    >
    > I wasn't clear there - I was reviewing source that came from an
    > outside origin - supposedly compiled correctly, and functional, but I
    > only have access to the source itself, not the build environment... So
    > I don't know if the original compile yielded any warnings to the
    > creator - but one would certainly hope it would in this case.
    >
    > I'm guessing not tho, as the functionality was flawed.


    It looks flawed to me. If they want it to execute every time just move it
    to before the switch() statement itself.
    Jim Langston, May 2, 2007
    #8
  9. On 2007-05-02 02:42, wrote:
    > On May 1, 2:47 pm, "Alf P. Steinbach" <> wrote:
    >> * :
    >>
    >>
    >>
    >> > Alright then - I know it's got to be out there somewhere, but I can't
    >> > find it...

    >>
    >> > I'm looking at a piece of code and there's some debate as to what the
    >> > behavior will be.

    >>
    >> > Given a switch/case where there is a function call after the switch
    >> > but *before* the first case statement, will the function call never
    >> > get reached? or always?

    >>
    >> > To whit:

    >>
    >> > switch (value) {

    >>
    >> > //this is where weird function call is... comment says run every time
    >> > randomfunction(x,y);

    >>
    >> > case 1:
    >> > cout << "foo!\n";
    >> > break;

    >>
    >> > case 2:
    >> > cout << "fah!\n";
    >> > break;

    >>
    >> > default:
    >> > cout << "foofah!\n";
    >> > break;
    >> > }

    >>
    >> > So what happens? does randomfuntion() get called every time? never?
    >> > The code has been successfully compiled... but no idea if there was a
    >> > warning or not.

    >>
    >> A switch statement is essentially a computed goto. Control transfers
    >> directly from the controlling expression to the corresponding label.
    >> From this you should be able to figure out what happens.
    >>
    >> --
    >> A: Because it messes up the order in which people normally read text.
    >> Q: Why is it such a bad thing?
    >> A: Top-posting.
    >> Q: What is the most annoying thing on usenet and in e-mail?

    >
    > Actually, put that way, it makes perfect sense.
    > I'd love to see how switch/case works on the machine level, but afraid
    > my Assembler is nigh-on non-existant! :p


    I'd imagine a lot of compare and jump, but I've read or heard somewhere
    that it's possible to optimize it to a indexing into an array of
    addresses. But it seems to me that you'll need really big switch
    statements for that to be an optimization.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, May 2, 2007
    #9
  10. shish Guest

    On May 1, 1:24 pm, wrote:
    > Alright then - I know it's got to be out there somewhere, but I can't
    > find it...
    >
    > I'm looking at a piece of code and there's some debate as to what the
    > behavior will be.
    >
    > Given a switch/case where there is a function call after the switch
    > but *before* the first case statement, will the function call never
    > get reached? or always?
    >
    > To whit:
    >
    > switch (value) {
    >
    > //this is where weird function call is... comment says run every
    > time
    > randomfunction(x,y);
    >
    > case 1:
    > cout << "foo!\n";
    > break;
    >
    > case 2:
    > cout << "fah!\n";
    > break;
    >
    > default:
    > cout << "foofah!\n";
    > break;
    > }
    >
    > So what happens? does randomfuntion() get called every time? never?
    > The code has been successfully compiled... but no idea if there was a
    > warning or not.
    >
    > Anyone?


    As far as I know, the function will never get called, as after the
    switch we will directly jump to the relevant case ..
    shish, May 2, 2007
    #10
    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. Dirk Meusel
    Replies:
    1
    Views:
    3,014
    Dirk Meusel
    Aug 19, 2003
  2. Michael Mair
    Replies:
    1
    Views:
    411
    S.Tobias
    Jun 14, 2005
  3. Chad
    Replies:
    7
    Views:
    323
    Boltar
    Jan 5, 2007
  4. Replies:
    11
    Views:
    574
  5. Mukesh
    Replies:
    4
    Views:
    622
    Paul N
    Mar 26, 2010
Loading...

Share This Page