Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!snorkelwacker!bloom-beacon!eru!luth!sunic!enea!sommar From: sommar@enea.se (Erland Sommarskog) Newsgroups: comp.lang.misc Subject: Re: Common subexpression optimization Message-ID: <767@enea.se> Date: 11 Feb 90 11:38:53 GMT References: <14229@lambda.UUCP> <2844@pkmab.se > Organization: Enea Data AB, Sweden Lines: 49 Kristoffer Eriksson (ske@pkmab.se) writes: >Jim Giles (jlg@lambda.UUCP) writes: >> int a[200][2]; int a[200][2]; int *p; int *q; >> ... ... >> ... p = &a; q = p+1; >> for (i=0;i<200;i++){ for (i=0;i<200;i++){ >> a[i][0] = expr1(i); *p = expr1(i); >> a[i][1] = expr2(i); *q = expr2(i); >> }; p += 2; q += 2; >> }; >> >>The first is clearly more readible than the second. ... > >Not that I necessarily agree with the first poster about always using >pointers for array loops, but why would you use *two* pointers? It >would be much more "natural" and less obscure this way: > >int a[200][2]; int *p; >p = &a[0][0]; /* Start fromthe first int of a. */ >for (i = 0; i < 200; i++) { > *(p++) = expr1(i); > *(p++) = expr2(i); >}; OK, I don't speak C, maybe this is a standard approach once you know the language, but I find Kristoffer's proposal to be at least as obscure as Jim Giles' right-hand example. I fail to see any problems with the left-hand fragment which is the obvious way to write it. The other two look as the programmer's been training for that obsfucated (or whatever the word is) C contest. >or this way: > >int a[200][2]; /* Array of 200 arrays of two int. */ >int (*p)[2]; /* Pointer to array of two int. */ >p = a; /* Start with the first array of two int of a. */ >for (i = 0; i < 200; i++) { > (*p)[0] = expr1(i); > (*p)[1] = expr2(i); > p++; /* Note automatic scaling. */ >} Slightly more understandable, but still obscure. It seems to assume that the dimension are stored in a certain order. Although this probably is defined by the language, it still seems unnecessary to rely on such a fact. Anyway, the human reader have to think a while before he can ensure that the fragment is correct. -- Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se Unix is a virus.