Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 65536 Switch Cases

Reply
Thread Tools

65536 Switch Cases

 
 
Bryan Parkoff
Guest
Posts: n/a
 
      09-16-2004
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-16-2004
"Bryan Parkoff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-16-2004
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-16-2004
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
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      09-16-2004
In article <(E-Mail Removed) >,
Bryan Parkoff <(E-Mail Removed)> 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?
 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-17-2004
"Bryan Parkoff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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>>
{
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


 
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
Cisco 851/%SYS-2-MALLOCFAIL: Memory allocation of 65536 bytes failed VAXman- @SendSpamHere.ORG Cisco 7 12-29-2008 12:12 PM
Multiple router eigrp 65536 - can't delete or edeit David Haggett Cisco 11 07-26-2007 04:25 AM
'break' statements to jump among switch cases: how to? z-man Java 7 10-05-2006 04:06 PM
Home server cases and external firewire cases thingy@nowhere.commy NZ Computing 5 03-14-2006 07:56 AM
Hundreds of cases in a switch, optimization? He Shiming C Programming 65 06-24-2005 07:18 PM



Advertisments