By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,952 Members | 1,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,952 IT Pros & Developers. It's quick & easy.

65536 Switch Cases

P: n/a
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.

--
Bryan Parkoff
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"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.


Personally, I find a 'switch' with more than perhaps a dozen
'cases' clumsy and too difficult to maintain. I suggest you
look into arrays (or e.g. 'std::map's) of pointers to functions
instead.

-Mike
Jul 22 '05 #2

P: n/a
Bryan Parkoff wrote:
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.


I think that if you seem to need more case labels than can fit in one
file with less than 1000 lines of code, your design is somewhat flawed.

Try to group those values and create functions to process ranges or
bits or ... Whatever you can come up with is very likely better than
64K case labels.

Victor
Jul 22 '05 #3

P: n/a
Bryan Parkoff wrote:
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.

--
Bryan Parkoff


The standard recommends that switch statements should be handled at least
up to 16384 labels. Unfortunately, this is not a requirement.

To give any recommendations about how to deal with the limitations, one
would need to know a little more about your particular situation, e.g., how
you are generating this huge switch statement and what it is supposed to
accomplish.
Best

Kai-Uwe Bux
Jul 22 '05 #4

P: n/a
In article <d5**************************@posting.google.com >,
Bryan Parkoff <Br**********@yahoo.com> wrote:
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.
Among many things, it might be possible you've run into a code
generator problems. It should be fairly easy (though not guaranteed)
to see at what value X for the # of switches that the particular
code (and compiler) will cease to work. If X seems to get ridiculously
small, it could be indicate of a bug in your code of course.
Either way, at that point you need to analyze what's gone wrong
and why.
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.


Most people don't need 65K switch statements, so you may want
to instead detail what it is that you are trying to accomplish,
and why, and why you feel such a switch is the preferred solution.
Otherwise, it appears you're tossing around arbitrary values
for X (and sub-X's), at best.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #5

P: n/a
"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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.