Function call after switch but before first case - behavior?

Y

yndygo

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?
 
M

Marcus Kwok

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.
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
V

Victor Bazarov

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
 
Y

yndygo

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.

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!
 
Y

yndygo

* (e-mail address removed):













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
 
Y

yndygo

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

V


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.
 
J

Jim Langston

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
S

shish

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 ..
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top