By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,276 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,276 IT Pros & Developers. It's quick & easy.

Function call after switch but before first case - behavior?

P: n/a
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 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
yn****@gmail.com 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
May 1 '07 #2

P: n/a
* yn****@gmail.com:
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?
May 1 '07 #3

P: n/a
yn****@gmail.com 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
May 1 '07 #4

P: n/a
On May 1, 2:41 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
ynd...@gmail.com 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 '07 #5

P: n/a
On May 1, 2:47 pm, "Alf P. Steinbach" <a...@start.nowrote:
* ynd...@gmail.com:
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 '07 #6

P: n/a
On May 1, 2:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
ynd...@gmail.com 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 '07 #7

P: n/a
<yn****@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
On May 1, 2:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>ynd...@gmail.com 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.
May 2 '07 #8

P: n/a
On 2007-05-02 02:42, yn****@gmail.com wrote:
On May 1, 2:47 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* ynd...@gmail.com:
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
May 2 '07 #9

P: n/a
On May 1, 1:24 pm, ynd...@gmail.com 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 ..

May 2 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.