switch and many case's. Any caveats?

R

Richard Damon

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.

It must accept all strictly conforming programs, but is only required to
translate and execute one of them. The compiler is allowed to fail to
translate any program (except that one special one for conformance) due
to exceeding translator resource limits, and those limits are NOT
restricted to the enumerated list, nor is not exceeding any of them an
assurance that a translation limit will not be hit.

One problem with the standard, is that it is leaves a lot of wiggle
room, and it is possible for a pathological compiler to pass the
conformance test due to the way the translation limit rules are written
(unless they have cleaned it up in the latest version).
 
T

Tim Rentsch

Richard Damon said:
It must accept all strictly conforming programs, but is only required to
translate and execute one of them.

Not even that. The mandated program need not be strictly conforming,
or even have defined behavior. So the bar for what an implementation
must be able to do in terms of which programs it necessarily will
translate is quite low.
The compiler is allowed to fail to
translate any program (except that one special one for conformance) due
to exceeding translator resource limits, and those limits are NOT
restricted to the enumerated list, nor is not exceeding any of them an
assurance that a translation limit will not be hit.

Yes, and that is as it should be, since the nature of those resources
is outside what the Standard concerns itself with, and explicitly so.

One problem with the standard, is that it is leaves a lot of wiggle
room, and it is possible for a pathological compiler to pass the
conformance test due to the way the translation limit rules are written
(unless they have cleaned it up in the latest version).

IMO this property is a problem only for people who are confused about
what it is the Standard sets out to do. The point of standardizing
"the C language" is to define what constitutes an implmentation of C.
Not what constitutes a _good_ implementation, but just what constitues
an implementation. The stipulation in 5.2.4.1 p1 serves that purpose
fairly well, I would say.

Furthermore, there is another important property, often overlooked,
that conforming implementations must satisfy. That property is
conditional correctness: if an implementation does accept a program
and translate it successfully, then when the program is executed (and
assuming the execution environment has sufficient resources to run the
program to completion), then the executable _must do the right thing_
(modulo hardware failures and OS bugs, which are outside the scope of
the Standard) as defined by the semantic descriptions in the Standard.
In other words, for any program that an implementation does translate,
the translation process must have no bugs. Moreover that includes not
just strictly conforming programs but _any_ program that has defined
behavior, including unspecified behavior or implementation-defined
behavior. In practical terms this more stringent requirement is both
more important and sufficient to provide a good level of quality in
all conforming implementations.
 
F

Francois Grieu

Enums also work quite nicely, as the compiler is able to perform some sanity
checks.

I can't figure out what these sanity checks are. This is C, not C++.

Francois Grieu
 
J

James Kuyper

I can't figure out what these sanity checks are. This is C, not C++.

At least one popular compiler (gcc) and probably others, has the ability
to check whether a switch statement based upon a value of enumerated
type has case labels that cover all of the associated enumeration
constants. If the appropriate option (-Wswitch or -Wswitch-enum) is
turned on, it will issue a warning if they do not. It will also warn if
a value outside the range of the enumerators is used for a case label.
No feature of C++ is needed to implement such warnings.
Possibly you're thinking of -Wenum-compare, which is C++ specific, but
has nothing to do with case labels.
 
T

Tim Rentsch

Francois Grieu said:
I can't figure out what these sanity checks are. This is C, not C++.

Surely all the checks for enum's that C++ does could also be done
by a C implementation, if the implementors chose to provide an
option for that; issuing diagnostics, for any reason, is always
conforming.
 
A

A. K.

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?

Giant switching is fairly common in virtual machines. The Forth guys
have spent a lot of investigation on it. Result: it depends too much on
the processor, branch prediction behaviour, caching, execution pipeline
stallment etc. So there is no generic answer to performance optimization.

If you want to be portable: take the switch
If you use gcc or vc: take computed goto's

And it your problem is really a VM: keep the virtual instruction pointer
aligned ie. don't use bytes but 32-bit integers. This can really boost
speed.
 
T

Tim Rentsch

why would this be made such an unusual exception to normal english usage?

Actually I believe it is standard usage in ordinary English when
the items referred to are textual entities of some sort. This
sentence, for example, has two This's, two and's, two eight's, two
example's, two for's, two has's, two sentence's, and eight two's.
We can count by twos, but the last sentence has eight two's, not
eight twos. Young children go through the terrible twos; it is
only later (we hope!) that they are taught two's complement. Or we
might say the decimal representation of 100 factorial has seven 7's
in it. From this point of view there is no exception in the two
cases illustrated above (and, please forgive me, pun intended).

Note that I'm not claiming this is right, only that it's my belief
that it's right (and it's been a rather long time since I last
opened my Chicago Manual of Style). Still, it does seem fair to
claim that the usage is -- at least! -- defensible.
 
B

Ben Bacarisse

Tim Rentsch said:
Actually I believe it is standard usage in ordinary English when
the items referred to are textual entities of some sort. This
sentence, for example, has two This's, two and's, two eight's, two
example's, two for's, two has's, two sentence's, and eight two's.
We can count by twos, but the last sentence has eight two's, not
eight twos. Young children go through the terrible twos; it is
only later (we hope!) that they are taught two's complement. Or we
might say the decimal representation of 100 factorial has seven 7's
in it. From this point of view there is no exception in the two
cases illustrated above (and, please forgive me, pun intended).

Lest this become too one-sided, Gower's "The Complete Plain Words" says
it should be avoided except where it's really needed to make the plural
obvious. The only case it endorses unequivocally is that of single
letters ("Mind your p's and q's"). It includes no special case of a
textual reference being made plural.

The "Oxford Dictionary of American Usage and Style" does not include
this use of the apostrophe. The only use of an apostrophe for plurals
is its use "to mark the plural of an acronym, number, or letter".

Fowler does not mention it.

And many reasonable writers (or editors) did not use this apostrophe.
For example, Shakespeare's "Tellest thou me of ifs", Centlivre's "But me
no Buts", and Kipling's "One million Hows, two million Wheres, And seven
million Whys". You might not consider these as references to "textual
entities of some sort". Maybe there are too metaphorical, despite being
reference to words.
Note that I'm not claiming this is right, only that it's my belief
that it's right (and it's been a rather long time since I last
opened my Chicago Manual of Style). Still, it does seem fair to
claim that the usage is -- at least! -- defensible.

I am sure there is support for this usage, but I like to see both sides
argued in this sort of case. That way, everyone can be right.
 
T

Tim Rentsch

Ben Bacarisse said:
Lest this become too one-sided,

It is always a pleasure to read your comments; I hope I can return
the favor.
Gower's "The Complete Plain Words" says
it should be avoided except where it's really needed to make the plural
obvious. The only case it endorses unequivocally is that of single
letters ("Mind your p's and q's"). It includes no special case of a
textual reference being made plural.

What does it say (or does it say anything) about textual references
otherwise? To me the important aspect is distinguishing the concept
from the text or word used to represent it, as might be done with
quotes, eg, "the variable x is a double" versus "x was declared
using 'double'".
The "Oxford Dictionary of American Usage and Style" does not include
this use of the apostrophe. The only use of an apostrophe for plurals
is its use "to mark the plural of an acronym, number, or letter".

All of these more or less correspond to the principle I am trying
to convey here - a word-as-word, or keyword, is treated as if it
were a single ideogram.
Fowler does not mention it.

You definitely outclass me in knowledge of matters style.

However, I didn't learn much from this statement, without
knowing what else Fowler might have to say about usage
when writing about words as text. I suppose I should
spend some time reading Fowler now for my own further
education if nothing else.
And many reasonable writers (or editors) did not use this apostrophe.
For example, Shakespeare's "Tellest thou me of ifs", Centlivre's "But me
no Buts", and Kipling's "One million Hows, two million Wheres, And seven
million Whys". You might not consider these as references to "textual
entities of some sort". Maybe there are too metaphorical, despite being
reference to words.

To me all of these are more references to the associated concepts
than to the particular words. Also the context is important -
we wouldn't expect the same kind of attention to such distinctions
in poetry or regular prose as we would in technical writing. In
technical writing the distinctions are often important; in poetry
the shape and sound of the words is typically more important than
removing confusion or ambiguity.
I am sure there is support for this usage, but I like to see both sides
argued in this sort of case. That way, everyone can be right.

Technically, I think you're right -- the usage rule I gave
is not officially sanctioned, at least not directly. What
I was meaning to claim is that this rule is often observed
in writing by technical people, and not just with respect
to keywords or programming terms.
 
B

Ben Bacarisse

Tim Rentsch said:
What does it say (or does it say anything) about textual references
otherwise?

Nothing, as far as I can tell, but it's not a simple concept to look up.

All of these more or less correspond to the principle I am trying
to convey here - a word-as-word, or keyword, is treated as if it
were a single ideogram.

Right. But I cut the quote off. It goes on to say that it's now more
usual to drop the apostrophe (except for letters).
You definitely outclass me in knowledge of matters style.

However, I didn't learn much from this statement, without
knowing what else Fowler might have to say about usage
when writing about words as text. I suppose I should
spend some time reading Fowler now for my own further
education if nothing else.

Turns out I was wrong, so no "outclassing" is possible. The apostrophe
for some plurals *is* mentioned, but in a section on possessives:

mps, the 1990s, etc. The apostrophe is no longer normally used in the
plural of abbreviated forms (e.g. Several MPs were standing around),
although it is of course used in the possessive (e.g. The BBC’s
decision to go ahead with the broadcast). It is used in plurals when
clarity calls for it, e.g. Dot your i's and cross your t's.

That's all. Much the same as the other advice, and nothing specifically
about textual references.

<snip>
 
T

Tim Rentsch

Ben Bacarisse said:
Right. But I cut the quote off. It goes on to say that it's now more
usual to drop the apostrophe (except for letters).

That is good to know. Thank you.
Turns out I was wrong, so no "outclassing" is possible. The apostrophe
for some plurals *is* mentioned, but in a section on possessives:

mps, the 1990s, etc. The apostrophe is no longer normally used in the
plural of abbreviated forms (e.g. Several MPs were standing around),
although it is of course used in the possessive (e.g. The BBC's
decision to go ahead with the broadcast). It is used in plurals when
clarity calls for it, e.g. Dot your i's and cross your t's.

That's all. Much the same as the other advice, and nothing specifically
about textual references.

For me that last quoted sentence is key, because the motivation
for using an apostrophe in the first place was clarity, ie, to
distinguish between a word used in its normal English sense and
the same "word" but used with a different, technical meaning.
 
O

Old Wolf

where I have a couple hundred case's. Does this degrade performance and if so
what would a better way?

It's a heck of a lot better than:
GOTO 400*(I=0)+440*(I=1)+490*(I=2)+600*(I=3)+630*(I=4)+
....
+6675*(I=213)
 

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

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top