65536 Switch Cases

Discussion in 'C++' started by Bryan Parkoff, Sep 16, 2004.

  1. 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
     
    Bryan Parkoff, Sep 16, 2004
    #1
    1. Advertising

  2. Bryan Parkoff

    Mike Wahler Guest

    "Bryan Parkoff" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Sep 16, 2004
    #2
    1. Advertising

  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.


    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
     
    Victor Bazarov, Sep 16, 2004
    #3
  4. Bryan Parkoff

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Sep 16, 2004
    #4
  5. Bryan Parkoff

    Greg Comeau Guest

    In article <>,
    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.


    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?
     
    Greg Comeau, Sep 16, 2004
    #5
  6. "Bryan Parkoff" <> wrote in message
    news:...
    > 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
     
    Ivan Vecerina, Sep 17, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z-man
    Replies:
    7
    Views:
    427
    Tor Iver Wilhelmsen
    Oct 5, 2006
  2. He Shiming

    Hundreds of cases in a switch, optimization?

    He Shiming, Jun 19, 2005, in forum: C Programming
    Replies:
    65
    Views:
    1,764
    Christian Bau
    Jun 24, 2005
  3. Lynn McGuire

    maximum cases in a switch ?

    Lynn McGuire, Aug 9, 2011, in forum: C++
    Replies:
    67
    Views:
    2,446
    James Kanze
    Aug 22, 2011
  4. Tom Graves

    zipimport limited to 65536 files?

    Tom Graves, May 1, 2014, in forum: Python
    Replies:
    0
    Views:
    74
    Tom Graves
    May 1, 2014
  5. Terry Reedy

    Re: zipimport limited to 65536 files?

    Terry Reedy, May 2, 2014, in forum: Python
    Replies:
    0
    Views:
    97
    Terry Reedy
    May 2, 2014
Loading...

Share This Page