Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > switch and many case's. Any caveats?

Reply
Thread Tools

switch and many case's. Any caveats?

 
 
Joe keane
Guest
Posts: n/a
 
      05-09-2012
In article <>,
Test <test@.nil.invalid.com> wrote:
>switch (i)
> {
> case 1:
> ...
> case 2:
> ...
> ...
> case 249:
> ...
> case 250:
> ...
> }


If this is really what you want to do, i think that's the best way to
write the code.

It would help if you could explain a bit why there are two hundred
cases. I have seen code where there are a couple dozen cases, and i
thought trying to 'modularize' it would result in code that is less
readable and probably slower.
 
Reply With Quote
 
 
 
 
Quentin Pope
Guest
Posts: n/a
 
      05-09-2012
On Tue, 08 May 2012 15:17:49 -0400, Eric Sosman wrote:

> On 5/8/2012 2:50 PM, Test wrote:
>> I am using a (some pseudo code):
>> switch (i)
>> {
>> case 1:
>> ...
>> case 2:
>> ...
>> ...
>> case 249:
>> ...
>> case 250:
>> ...
>> }
>>
>> where I have a couple hundred case's. Does this degrade performance and
>> if so what would a better way?

>
> The original ANSI "C90" Standard required compilers to support at
> least 257 case labels per switch, and the "C99" revision raised the
> limit to 1023. So with "a couple hundred" cases you're safe, but with
> "several hundred" you might get into trouble with C90 compilers. (A
> particular C90 compiler may allow more than 257 cases, but the Standard
> does not require all C90 compilers to do so.)


I believe the requirement is that it must accept *at least one*
conforming program with this number of case labels, so there is no
guarantee you're safe for any particular program.
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      05-10-2012
jgharston <> writes:

> Test wrote:
>> where I have a couple hundred case's.

>
> The plural of case is cases.
> cat cats, dog dogs, table tables, tree trees, fire fires, case cases.


I believe the usage is defensible, to distinguish between
"case" as an English word and 'case' as a C keyword. So
for example

switch(n%10){
case 0:
case 1: x = 0; break;

case 2:
case 3:
case 5:
case 7: x = 1; break;

case 4:
case 6:
case 8:
case 9: x = 3; break;

}

There are three cases, but 10 case's.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      05-10-2012
Quentin Pope <> writes:

> On Tue, 08 May 2012 15:17:49 -0400, Eric Sosman wrote:
>
>> On 5/8/2012 2:50 PM, Test wrote:
>>> I am using a (some pseudo code):
>>> switch (i)
>>> {
>>> case 1:
>>> ...
>>> case 2:
>>> ...
>>> ...
>>> case 249:
>>> ...
>>> case 250:
>>> ...
>>> }
>>>
>>> where I have a couple hundred case's. Does this degrade performance and
>>> if so what would a better way?

>>
>> The original ANSI "C90" Standard required compilers to support at
>> least 257 case labels per switch, and the "C99" revision raised the
>> limit to 1023. So with "a couple hundred" cases you're safe, but with
>> "several hundred" you might get into trouble with C90 compilers. (A
>> particular C90 compiler may allow more than 257 cases, but the Standard
>> does not require all C90 compilers to do so.)

>
> I believe the requirement is that it must accept *at least one*
> conforming program with this number of case labels, so there is no
> guarantee you're safe for any particular program.


Not exactly. The Standard requires conforming implementations
to accept any strictly conforming program. A program that is
strictly conforming in all other regards, and also uses no more
than 1023 case labels in any switch() statement, is strictly
conforming and so must be accepted by any conforming implementation.
 
Reply With Quote
 
Quentin Pope
Guest
Posts: n/a
 
      05-10-2012
On Thu, 10 May 2012 10:12:11 -0700, Tim Rentsch wrote:

> Quentin Pope <> writes:
>
>> On Tue, 08 May 2012 15:17:49 -0400, Eric Sosman wrote:
>>
>>> On 5/8/2012 2:50 PM, Test wrote:
>>>> I am using a (some pseudo code):
>>>> switch (i)
>>>> {
>>>> case 1:
>>>> ...
>>>> case 2:
>>>> ...
>>>> ...
>>>> case 249:
>>>> ...
>>>> case 250:
>>>> ...
>>>> }
>>>>
>>>> where I have a couple hundred case's. Does this degrade performance
>>>> and if so what would a better way?
>>>
>>> The original ANSI "C90" Standard required compilers to support at
>>> least 257 case labels per switch, and the "C99" revision raised the
>>> limit to 1023. So with "a couple hundred" cases you're safe, but with
>>> "several hundred" you might get into trouble with C90 compilers. (A
>>> particular C90 compiler may allow more than 257 cases, but the
>>> Standard does not require all C90 compilers to do so.)

>>
>> I believe the requirement is that it must accept *at least one*
>> conforming program with this number of case labels, so there is no
>> guarantee you're safe for any particular program.

>
> Not exactly. The Standard requires conforming implementations to accept
> any strictly conforming program. A program that is strictly conforming
> in all other regards, and also uses no more than 1023 case labels in any
> switch() statement, is strictly conforming and so must be accepted by
> any conforming implementation.


5.2.4.1
"The implementation shall be able to translate and execute at least one
program that contains at least one instance of every one of the following
limits: ..."

This is quite a weak guarantee.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-10-2012
On 05/10/2012 04:28 PM, Quentin Pope wrote:
> On Thu, 10 May 2012 10:12:11 -0700, Tim Rentsch wrote:
>
>> Quentin Pope <> writes:
>>
>>> On Tue, 08 May 2012 15:17:49 -0400, Eric Sosman wrote:
>>>
>>>> On 5/8/2012 2:50 PM, Test wrote:
>>>>> I am using a (some pseudo code):
>>>>> switch (i)
>>>>> {
>>>>> case 1:
>>>>> ...
>>>>> case 2:
>>>>> ...
>>>>> ...
>>>>> case 249:
>>>>> ...
>>>>> case 250:
>>>>> ...
>>>>> }
>>>>>
>>>>> where I have a couple hundred case's. Does this degrade performance
>>>>> and if so what would a better way?
>>>>
>>>> The original ANSI "C90" Standard required compilers to support at
>>>> least 257 case labels per switch, and the "C99" revision raised the
>>>> limit to 1023. So with "a couple hundred" cases you're safe, but with
>>>> "several hundred" you might get into trouble with C90 compilers. (A
>>>> particular C90 compiler may allow more than 257 cases, but the
>>>> Standard does not require all C90 compilers to do so.)
>>>
>>> I believe the requirement is that it must accept *at least one*
>>> conforming program with this number of case labels, so there is no
>>> guarantee you're safe for any particular program.

>>
>> Not exactly. The Standard requires conforming implementations to accept
>> any strictly conforming program. A program that is strictly conforming
>> in all other regards, and also uses no more than 1023 case labels in any
>> switch() statement, is strictly conforming and so must be accepted by
>> any conforming implementation.

>
> 5.2.4.1
> "The implementation shall be able to translate and execute at least one
> program that contains at least one instance of every one of the following
> limits: ..."
>
> This is quite a weak guarantee.


True, but that's not the promise Tim is talking about. 4p6 says "A
conforming hosted implementation shall accept any strictly conforming
program." Depending upon how "accept" is defined, that could be a very
strong guarantee, making up for the weakness of 5.2.4.1 - but the
standard fails to define "accept".

Some people have argued that "accept" must be equivalent to "translate
and execute". However, if that were the case, there would be no such
thing as a conforming implementation, because it's a relatively
straightforward task to describe a strictly conforming program that will
exceed the capacity of any particular implementation to handle it.
5.2.4.1 doesn't place any limits, explicit or implicit, on the total
size of a program, whether measured in terms of characters of source
code or in terms of tokens after phase 4 is complete. There's not many
implementations out there, if any, which could accept, in that sense, a
strictly conforming but arbitrarily long program with non-trivial
semantics that met every upper limit listed in 5.2.4.1 - not just once,
like the "one program", but at every possible opportunity.

Creating such a program would be more of a problem than describing it.
Any program which is too complex for a C implementation to parse, is
probably going to be very difficult to generate, too.

The ordinary English usage of "accept" allows me to say that I've
accepted a gift as long as I took it from the hands of the giver and
thanked that person for the gift, even if I never opened the gift, and
threw it away as soon as the giver was out of sight. Similarly, consider
a system which:

1. Always issues a diagnostic message saying "Your program may contain
defects" to satisfy 5.1.1.3p1.
2. Parses the program though phases 1-4 to determine whether any #error
directive survived conditional processing. If so, it issues the
specified error message, satisfying 6.10p5, and does nothing else with
it, satisfying 4p4. Otherwise, it issues a message saying "program
accepted", satisfying 4p6.
3. Checks to see whether the program is an exact match for the "one
program" mentioned in 5.2.4.1, and if so, translates and executes it
correctly. Otherwise, it does nothing more with it.

I contend that such a system could conform to all of the actual
requirements of the C99 standard (rather than the intended requirements,
which I gather were a bit stronger). C2011 seems to contain all of the
same weasel wording that allowed this to be a conforming C99
implementation, but I haven't had time to study it in detail to be sure
about that.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      05-10-2012
On Thu, 10 May 2012 20:28:08 +0000, Quentin Pope wrote:

> 5.2.4.1
> "The implementation shall be able to translate and execute at least one
> program that contains at least one instance of every one of the following
> limits: ..."
>
> This is quite a weak guarantee.


AIUI, the intent is to avoid requiring an implementation to handle the
worst-case combinations of limits, e.g. a structure having 1023 members, each of
which is a structure having 1023 members, and so on, nested to 63 levels
(i.e. 1023^63 members in total), or a function with 127 parameters, each
of which has such a pathological structure as its type.

IOW, if the implementation has fixed limits, they can't be any lower than
the values specified, but it doesn't have to support every possible
combination of those limits.

 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      05-11-2012
On Thursday, May 10, 2012 5:52:26 PM UTC+1, Tim Rentsch wrote:
> jgharston <> writes:
>
> > Test wrote:
> >> where I have a couple hundred case's.

> >
> > The plural of case is cases.
> > cat cats, dog dogs, table tables, tree trees, fire fires, case cases.

>
> I believe the usage is defensible, to distinguish between
> "case" as an English word and 'case' as a C keyword. So
> for example
>
> switch(n%10){
> case 0:
> case 1: x = 0; break;
>
> case 2:
> case 3:
> case 5:
> case 7: x = 1; break;
>
> case 4:
> case 6:
> case 8:
> case 9: x = 3; break;
>
> }
>
> There are three cases, but 10 case's.


why would this be made such an unusual exception to normal english usage?
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      05-11-2012
Quentin Pope <> writes:

> On Thu, 10 May 2012 10:12:11 -0700, Tim Rentsch wrote:
>
>> Quentin Pope <> writes:
>>
>>> On Tue, 08 May 2012 15:17:49 -0400, Eric Sosman wrote:
>>>
>>>> On 5/8/2012 2:50 PM, Test wrote:
>>>>> I am using a (some pseudo code):
>>>>> switch (i)
>>>>> {
>>>>> case 1:
>>>>> ...
>>>>> case 2:
>>>>> ...
>>>>> ...
>>>>> case 249:
>>>>> ...
>>>>> case 250:
>>>>> ...
>>>>> }
>>>>>
>>>>> where I have a couple hundred case's. Does this degrade performance
>>>>> and if so what would a better way?
>>>>
>>>> The original ANSI "C90" Standard required compilers to support at
>>>> least 257 case labels per switch, and the "C99" revision raised the
>>>> limit to 1023. So with "a couple hundred" cases you're safe, but with
>>>> "several hundred" you might get into trouble with C90 compilers. (A
>>>> particular C90 compiler may allow more than 257 cases, but the
>>>> Standard does not require all C90 compilers to do so.)
>>>
>>> I believe the requirement is that it must accept *at least one*
>>> conforming program with this number of case labels, so there is no
>>> guarantee you're safe for any particular program.

>>
>> Not exactly. The Standard requires conforming implementations to accept
>> any strictly conforming program. A program that is strictly conforming
>> in all other regards, and also uses no more than 1023 case labels in any
>> switch() statement, is strictly conforming and so must be accepted by
>> any conforming implementation.

>
> 5.2.4.1
> "The implementation shall be able to translate and execute at least one
> program that contains at least one instance of every one of the following
> limits: ..."


I'm familiar with 5.2.4.1. It doesn't change the point I was
making, namely, that your claim about what the requirements are
for what implementations must accept is wrong.

> This is quite a weak guarantee.


It is neither a weak guarantee nor a strong guarantee, because it
is not a guarantee. This sentence, along with almost every other
sentence in the Standard, is simply part of the definition of what
constitutes a conforming implementation of ISO C. Definitions
don't make promises; they are just definitions.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      05-11-2012
Tim Rentsch於 2012年5月11日星期五UTC+8上午12時52分26 寫道:
> jgharston <> writes:
>
> > Test wrote:
> >> where I have a couple hundred case's.

> >
> > The plural of case is cases.
> > cat cats, dog dogs, table tables, tree trees, fire fires, case cases.

>
> I believe the usage is defensible, to distinguish between
> "case" as an English word and 'case' as a C keyword. So
> for example
>
> switch(n%10){
> case 0:
> case 1: x = 0; break;
>
> case 2:
> case 3:
> case 5:
> case 7: x = 1; break;
>
> case 4:
> case 6:
> case 8:
> case 9: x = 3; break;
>
> }
>
> There are three cases, but 10 case's


I suggest you can use the following method to speed up!

s=1<<n; //

if (s&(1|2)) { ..... // case 0 or 1 }
else if (s&(4|8|32|12) { ....// case 2,3,5,7 }
else if (s&(16|64|256|512)){ ... // };


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trunking between Cisco Catalyst 500 switch and other type of switch jshubo@yahoo.com Cisco 3 01-19-2008 12:10 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Is a frame switch and an ISDN switch really needed? owmanstubbedmytoe Cisco 2 12-05-2004 07:15 AM
difference b/w layer 2 switch and layer 3 switch praveen Cisco 1 10-22-2003 07:19 AM
switch hub and switch will NZ Computing 6 10-19-2003 10:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57