Just started teaching myself C++ about 3 days ago, but have my goals well defined. My question concerns switch optimization and jump tables. Given a switch with case values in a narrow range the compiler will optimize it as a jump table. If you include an OR condition (by leaving out a break) such as:
1 2 3 4 5 6 7 8 9 10 11
switch (num)
{
case 0:
<do stuff>
case 1:
<do stuff>
break;
case 2:
<do stuff>
break;
}
Would this break the compilers jump table optimization? As an educated guess I presume no, since a branch table merely defines an entry point (like goto) and a second case lacking any break statements should simply get ignored as usual. I'm still haveing fits data types but I'm getting there. Sometimes knowing specifically how to break something is more informative than how something works. Comments would be appreciated.
The break statements simply tell the program when to return to the normal program flow. By removing one break statement you are telling the program to continue through the switch statement. So say that num was 0 and the program reached your switch. Case 0 would happen and then case 1 would happen. After it finished case 1 it would jump to the code below the switch. Hope that helped.
Yes, I understand the switch as an algorithm. What I was interested in is the compilers optimization. For a set of switch cases that exceed a certain narrow range of values the compiler will default to an if else if structure. This forces program execution to test every case, both before and after finding a matching case. When the case value ranges are within a certain range, etc., the compiler will instead structure the switch as a 'branch table'. This means program execution will jump directly to the matching case, without the need for testing each case individually. It is this 'branch table' feature that I didn't want to break. However, if I can stack cases, without breaking the 'branch table', I can avoid a lot of if conditions and variables within individual cases.
So my question is: Does stacking case conditions, without the 'break;', also break the 'branch table' feature of the compiler? I can see no reason why it should, but have no way to test it without very significant effort.