Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!accelerator.eng.ohio-state.edu!kaa.eng.ohio-state.edu!rob From: rob@kaa.eng.ohio-state.edu (Rob Carriere) Newsgroups: comp.lang.c Subject: Re: Numerical Recipes is non-portable code Message-ID: <564@accelerator.eng.ohio-state.edu> Date: 5 Sep 88 01:33:44 GMT References: <557@accelerator.eng.ohio-state.edu> <15029@ism780c.isc.com> Sender: news@accelerator.eng.ohio-state.edu Reply-To: rob@kaa.eng.ohio-state.edu (Rob Carriere) Organization: Ohio State Univ, College of Engineering Lines: 54 In article <15029@ism780c.isc.com> marv@ism780.UUCP (Marvin Rubenstein) writes: >In article <557@accelerator.eng.ohio-state.edu> rob@kaa.eng.ohio-state.edu >(Rob Carriere) writes: >> [ still on non-0 base arrays ] >Could you give a small example, perhaps I don't understand. with a >declaration (using an extended version of C): > > int x(5:49),y(0:44); > int i,k; > i=6; > k=x(i); /* easy to write access to second item */ > k=y(i-5); /* more trouble to write access to second item */ > >However, with a reasonable compiler, both assignments to k will generate >identical code. Your compiler may vary :-). But if you are writing time >critical code with a faulty compiler, I guess you do what you can. You more or less have the idea (except that the array would be float, I'm one of them there dirty numerical geeks :-). Actually, a reasonable (as opposed to a *very* good) compiler would probably generate better code for k=x[i]; because it cannot do the optimization I'm trying to manually make either: illegal pointer! To add insult to the poor compiler's injury, in most cases the code would actually read: void foo( int n ) { float *x; int i; float k; i = 6; x = vector( 5, n ); ... k = x[i]; ... } ... foo( 49 ); As a further and at least as important point, the code is generated from some underlying math that insists that x starts at (say) 5. So I can fake it and confuse everyone with my constant -5 offsets in all the indeces, or I can use a non-zero base offset. If you are curious as to what perverse math would generate such stuff (and it's *not* because they don't know to start counting at zero!), I sent off a mail message with a couple of examples to Marty Fouts who asked the same question. It's a bit too long and too marginal to this group to post, but I'd gladly send you a copy (be sure to give me a *very* complete email address, our mail deamon spends far more time earning its last than its first name!) Rob Carriere