473,388 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

65536 Switch Cases

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
5 3087
"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob Adelberg | last post by:
I have a single switch statement with about 30 case statements. I could break the single switch into 3 or 4 switchs, which logically would be more efficient. switch() { switch() // only 10...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
8
by: Dave | last post by:
Hello all, Consider the case of an if with many else if clauses vs. a switch with many cases. In thinking about how such control constructs would likely be implemented, it would seem that...
25
by: Ian Tuomi | last post by:
Is it possible to go through multiple cases in one case like this? how? switch(foo) { case 1 or 2: dosomething(); break; //dosomething if foo is either 1 or 2 case 3: dosomethingelse();...
11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
9
by: wouter | last post by:
hey hi..... I wanna make a switch wich does this: if pagid is set do A, if catid is set do B, if projectid is set do C, else do D. So i was thinking something like this:
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.