"Bryan Parkoff" <Br**********@yahoo.com> wrote in message
news:d5**************************@posting.google.c om...
C++ programmers and I tried to experience by writing 65536 switch
cases. It is too large for Windows XP and other operating system to
handle. It looks like that JMP Table obtains 256K bytes for 65536
switch cases. The program would crash when it attempts to run.
C++ Compiler does not support above 64K switch cases. I tried to
set a limit to 2048 switch cases and 4096 switch cases, but the
problem exists when program crashed again.
Do you recommend that switch cases should be limited to 300? Then
create 8 to 16 sub-switch cases inside 300 switch cases that it would
help to avoid crashes.
Please comment what you think.
In my (draft) of the C99 standard, the minimal number of switch cases
that a compiler needs to support (if another limit such as maximum
function size is not hit) is 1023. (see 5.2.4.1/1)
As Kai-Uwe said, the C++ standard recommends a limit no lower than
16384 (Annex B/2), but this is not a strict requirement.
In practice, no one would like to maintain a switch statement with
65k cases. A practical alternative, for example when decoding
processor instructions in an emulator (which are actually designed
for this), is to decompose the bits of the switch value in two halves:
switch(v>>8)
{
case 0x00: return doSwitch00(v);
case 0x01: return doSwitch01(v);
...
case 0xFF: return doSwitchFF(v);
}
then e.g.:
int doSwitch00(int v)
{
switch(v&0xFF) {
case 0x00: ....etc
}
}
You could also see if your compiler tolerates 64k cases in a 2-level
nested switch, and inline the second switch statements into the first one.
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form