Re: switch() Statement Question



Rob Clarke wrote:
Ben Bacarisse scribbled:
Han from China is messing you (and everyone else) about. Review is
posting history and decided how long you want to spend debating this
point.

Ah, I should have caught that. I am curious now, though: How many `case' labels can a strictly conforming program get away with using? If it's not the patently
absurd 0, as Han from China seems to be suggesting, what does the standard actually guarantee?

C90 guarantees 257 case labels, C99 guarantees 1023. As I read
it, these do not include a possible default: label.

The "guarantees," however, are a little weaker than might be
supposed. Here's the relevant C99 text (5.2.4.1p1):

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:

.... which is the Standard's acknowledgment that different features
of a program may put stress on the same parts of an implementation.
For example, there's a C99 "guarantee" of at least 127 levels of
nested blocks, but it does not follow that you can successfully
compile a program where a function's body (outermost block) contains
a switch with 1024 branches (1023 cases plus default), each branch
containing another similar switch, and so on down through 126 levels
of switch for 1024 + 1024^2 + ... + 1024^126 = (1024^127 - 1) / 1023
or approximately 2E379 branches in all! (One immediate difficulty
is that there's not enough disk space on Planet Earth to hold the
source code ...)

Some people (and I see by the quoted material that you've run
into one of them) have used this "one program, one instance" text
to support a specious argument that there are no useful guarantees.
The idea is that an implementation could fulfill the guarantees by
exhibiting a fixed set of programs that collectively exercised all
the limits, and then rejecting all other source codes as "too big."
Okay, fine, they've had their fun. One only wishes that they wouldn't
keep cackling over the same tired old joke.

--
Eric.Sosman@xxxxxxx
.



Relevant Pages

  • Re: hwpmc granularity and 6.4 network performance
    ... It's not so big in terms of rule count and not so big in terms of exact hint, ... I've heard about a pmcannotate tool, in -arch@, and I think that it is tool ... bunch of 'case's in a large 'switch'. ... so kgdb does know where are instructions for each line of source code. ...
    (freebsd-performance)
  • Re: CreateThread Fails with ERROR_NOT_ENOUGH_MEMORY....
    ... I dont think its running out of system memory. ... Using the /3G switch will actually reduce the number of ... There are other limits (total number of HANDLEs, ... the app has almost 180 threads & the process VM has shoot upto 1.9GB. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Why doesnt "switch" statement have "break" as default?
    ... The 'C' switch statement is in no way syntactic sugar for a cascade of if-else ... I do realise that the compiler and/or JIT is allowed to implement any ... As to the proposal to add lists and ranges, a couple of comments, not entirely ... and those limits would translate into apparently arbitrary (and very ...
    (comp.lang.java.programmer)
  • Re: "Did not find leading dereferencer" - new findings to an old puzzle
    ... use Switch would have had the same effect). ... the "leading dereferencer" error disappears and instead I see the ... It definitely does not need to parse the source code of the whole ... unless we also assume a bug in the Perl compiler itself. ...
    (comp.lang.perl.misc)
  • Re: landing weather
    ... landing, because showers popped up on the approach end of runway 33. ... crosswinds had increased past limits? ... It was nice to see how smoothly the runway heading switch was handled - does ...
    (sci.space.shuttle)