473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Function call after switch but before first case - behavior?

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
9 1878
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
* 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
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
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
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
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
<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: Alfonso Morra | last post by:
I have come accross some code where function pointers are being cast from one type to another (admittedly, they all have the same return type). eg. you may have func ptrs declr as ff: typedef...
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
20
by: sam_cit | last post by:
Hi Everyone, I have the following code, int main() { int p = {10,20,30,40,50}; int *i = &p; printf("before : %p\n",(void*)i); printf("1 %d %d\n",*++i,*i++);
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
34
by: Srinu | last post by:
Hi all, Can we assign return value of a function to a global variable? As we know, main() will be the first function to be executed. but if the above is true, then we have a function call before...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.