473,785 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3143
"Bryan Parkoff" <Br**********@y ahoo.com> wrote in message
news:d5******** *************** ***@posting.goo gle.com...
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**********@y ahoo.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**********@y ahoo.com> wrote in message
news:d5******** *************** ***@posting.goo gle.com...
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
4280
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 cases switch() // only 10 cases
35
8359
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 for '5'. So in his switch statement, he omits a case for '5':
8
6826
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 neither would be superior to the other in terms of execution speed of the generated code. Would others agree that this is true for "typical" implementations?
25
2644
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(); break; // dosomethingelse if foo is 3 }
11
3771
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 the assembly level ? for a performance critical application is it better to to use switch case or accomplish the same using fn pointers ?
65
6702
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? In terms of generated machine code, how does hundreds of cases in a switch differ from hundreds of if-elses? Do compilers or processors do any optimization on such structured code? Do I need to worry about the performance, usually?
9
9742
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
4751
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 that it makes switch-case faster than if-else statements, is it true and if so what is the underlying concept behind a switch-case compilation? Can anyone help in this regard. Thanks in advance!!!
13
11847
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 just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.