oz@yetti.UUCP (Ozan Yigit) (02/20/86)
Why is it, when someone asks whether or not jumptables with goto labels are allowed in C, there is the overwhelming response "use a case statement" ???? It is not the same, except in one isolated circumstance, where the user-created jumptable is completely static. Furthermore: [1] You are never guaranteed to get a jumptable out of a case statement. If you get it with compiler X, you may not get with compiler Y. [the reason may be a buggy implementation or whatever, but would be unnoticable, since it does not effect the correctness of the generated code] [2] The structure of the case jumptable may be different from compiler to compiler. [Vaxen Cc may generate a jump table of relative offsets to feed to case[wl] instruction, whereas others may generate a table of direct addresses] [3] The jumptable is hidden, thus cannot be manipulated. (unless, of course, you are practicing black magic :-) I can see some weird application where a jumptable of goto labels is used, and program dynamically alters the control flow by swapping labels on the jump table, or by adding new ones in. Surely this would be most unreadable, and difficult to debug, and something I would classify as "pornographic programming", but than, It is proven nicely in "obfuscated C" contests that one can do a lot worst. Please hold your flame, in case you are ready to lit. I am not suggesting that C should have the capability, [Although it is certainly debatable] nor am I suggesting that a jumptable with goto labels is necessary, where jumptables with function entries could easily be created. All I am arguing against is the unthinking suggestion that case == jumptable of labels. Oz -- Usenet: [decvax|allegra|linus|ihnp4]!utzoo!yetti!oz Bitnet: oz@[yusol|yuyetti] FREE SOFTWARE FOUNDATION vs. EMPIRE. Watch for things to come..
gnu@hoptoad.uucp (John Gilmore) (02/25/86)
It is true that a case (in C, "switch") statement is not the same as a jump vector, because the vector cannot be modified. However, a trivial hack gives the behaviour you want: int vector[MAX_JUMPS]; switch (vector[i]) { This costs one subscripting operation per jump -- a price, but a small price. Not nearly the overhead of a function call, for example. I've seen exactly this kind of contruct used for the inner loop of an interpreter. It probably works OK for a regular expression scanner, or lexer, too. You can even do it one better by declaring int *vector; and pointing it at whatever jump table you are currently interested in, out of a set of precompiled or dynamically built jump tables. Are we having done yet? -- John Gilmore {sun,ptsfa,lll-crg,ihnp4}!hoptoad!gnu jgilmore@lll-crg.arpa