Switch case - limit to number of cases?

Posted By: Dalla

Switch case - limit to number of cases? - 10/26/17 19:10

Hi,

I have a HUGE switch case statement with more than 2000 cases.
It seems like once I go above 2090 switch statements, my script crashes.

Is this a known limit?

I´ve tried debugging with verbose = 31, but there´s nothing in the log really except the crash.
Posted By: AndrewAMD

Re: Switch case - limit to number of cases? - 10/26/17 21:16

The attached script uses a switch with 3000 elements and it works fine.

Isolate your switch and figure out what's messed up.

Attached File
bigswitch.c  (9 downloads)
Posted By: Hredot

Re: Switch case - limit to number of cases? - 10/26/17 23:54

I usually get a crash when I try to access more data into the past than I reserved in LookBack variable. Just a possibility! =)
Posted By: Dalla

Re: Switch case - limit to number of cases? - 10/27/17 07:30

Well, in my case this works

Code:
case 2073: return closeDevMA10N[0] < 0.0; break;
	case 2074: return closeDevMA10N[0] < -0.2; break;
	case 2075: return closeDevMA10N[0] < -0.4; break;
	case 2076: return closeDevMA10N[0] < -0.6; break;
	case 2077: return closeDevMA10N[0] < -0.8; break;
	
        ....
	case 2088:  
	case 2089: 
	case 2090: return true; break;



while this does not
Code:
case 2073: return closeDevMA10N[0] < 0.0; break;
	case 2074: return closeDevMA10N[0] < -0.2; break;
	case 2075: return closeDevMA10N[0] < -0.4; break;
	case 2076: return closeDevMA10N[0] < -0.6; break;
	case 2077: return closeDevMA10N[0] < -0.8; break;
	
        ....
	case 2088:  
	case 2089: 
	case 2090: 
	case 2091: return true; break;



So basically adding one more switch statement crashes the script
Posted By: Ayumi

Re: Switch case - limit to number of cases? - 10/27/17 10:18

You do not need any break with return...and btw, so many cases are bad programming.

EDIT:
I remember this thread. Maybe it's the same ....
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=467181#Post467181
Posted By: Dalla

Re: Switch case - limit to number of cases? - 10/27/17 14:00

Probably it is, but still doesn´t explain why it would crash.

Unfortunately I don't know german...
Posted By: AndrewAMD

Re: Switch case - limit to number of cases? - 10/27/17 15:04

It is not necessary to know German. Summary: your function is too large. Large functions are bad programming practice.

You have many good alternatives. Here's one:
* Use a csv file to store your long list of values.
* When the script starts, load the csv file into a var array or struct array.
* Pull values from your array as needed.
Posted By: jcl

Re: Switch case - limit to number of cases? - 10/30/17 10:28

The summary of the German thread: For statements like if, while, switch, etc. the compiler generates an internal jump table on the stack. Which means that the stack, since it is finite, can overflow at some point, dependent on what else is on the stack.

This has no relevance for real programming since no one would ever have 2000 nested if() clauses or a switch with 2000 cases... or so I thought. But apparently lite-C programmers break all sorts of strange code records.

Conclusion: Code in a reasonable way.
Posted By: Dalla

Re: Switch case - limit to number of cases? - 10/30/17 11:09

Got it :-)
© 2024 lite-C Forums