mildly obfuscating c

Joe Buck jbuck at epicen.UUCP
Tue Dec 10 17:27:46 AEST 1985

In article <564 at puff.UUCP> tom at puff.UUCP writes:
>ok, guys, now i will admit that the below code is *not* kosher. 
>but the question still remains, if you run this program, what will
>your output be?  does the machine you compile it on make
>a difference?  does defining ARG to be something, say 999,
>make a difference?   what if VAR were auto or static?
># define ARG
># define VAR register
># define CALL(x) (*(int (*)()) (x))(ARG)
>main() {
>    VAR thing = 0;
>    printf("here it goes, thing is %d\n",thing);
>    if (!thing++) 
>        CALL(stuff);
>    printf("one there it went, thing is %d\n",thing);
>    printf("two there it went, thing is still %d\n",thing);
>/* lint outputs
>test.c(8): warning: questionable conversion of function pointer
>note the lint output.  no kidding.
>on a vax, i get a reserved intruction trap as soon as i call the

On a Vax, subroutines called by the CALLS instruction (which is what
the compiler generates) start with a two-byte register save mask. There
isn't one at the label, rather there's some random pattern, hence the
trap. I can't give details for other machines, but you can rest assured
that registers and stack won't be saved and restored properly in most cases.
That's the real reason this isn't valid.

>i haven't been able to figure out anyway to "goto" a label that
>i don't know.  for example, i would like to do this:
>main() {
>	int (*jump[])() = { l1,l2,l3,l4,l5,l6,l7,l8 }
>	l1: /* code */
>	l2: /* code */
>	l8: /* code */
>	goto *jump[whatever]
>aside from the forward-referencing problem of the unseen labels, 
>this is a still syntax error.   anyone have any way to do this?

Sure do.

	switch(whatever) {
	case 0: /* code at l1 above */
		break; /* unless your intention is to fall through
			  to the next case */
	case 1: /* code at l2 above */
	case 7: /* code at l8 above */

etc. This should have identical effect, and will compile into code
that looks very much like the above in many cases.

There are rare situations where goto's are reasonable (I never use
them myself), but I really think you should get out of the habit of
thinking that way.
Joe Buck				|  Entropic Processing, Inc.
UUCP: {ucbvax,ihnp4}!dual!epicen!jbuck  |  10011 N. Foothill Blvd.
ARPA: dual!epicen!jbuck at BERKELEY.ARPA   |  Cupertino, CA 95014

More information about the Comp.lang.c mailing list