Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!rutgers!cmcl2!lanl!jlg From: jlg@lanl.gov (Jim Giles) Newsgroups: comp.lang.misc Subject: Re: s Message-ID: <4423@lanl.gov> Date: 30 Oct 90 19:15:43 GMT References: <8960019@hpfcso.HP.COM> Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 45 From article <8960019@hpfcso.HP.COM>, by mjs@hpfcso.HP.COM (Marc Sabatella): > [...] > int array[100]; int array[100]; > int *p; int i; > extern int *bar(); extern int bar(); > > foo () foo () > { { > p = bar(); i = bar(); > *p = 42; array[i] = 42; > } } > [...] > Tough to get code for foo() in the second case to be as good as code in the > first case without more interprocedural data flow analysis than most compilers > are capable of. [...] Yes, but the problem is not whether foo() in isolation is more efficient but whether the _program_ as a whole is more efficient. In this case, in order for these two programs to have the same semantics, the base of array[] must be added into p. Whether this add is done in foo() or in bar(), it still takes the same time to do. Further, if the assignment to array[] involved an expression more complicated than '42' (like, having references to other arrays for example), then the pointer version would not be able to optimize as well since it can't know from pointers whether they are within the same or different arrays. So, in general the "pointer-free" version will be faster. At worst, it will be the same. > [...] Especially if we extend the problem and let "p" optionally > point into either of two arrays, where "bar" reads from stdin to determine > which array to point into. [...] In this case, bar() could be given array[] as an argument which has the "aliased" attribute turned on. Then, bar() could use the "shallow copy" assignment to select which of the two arrays that array[] really represented. As you mentioned, this is bad coding practice (usually), but if you need it, you can do it. Note: as I've repeatedly pointed out, "aliased" variables are _identical_ in semantics to Pascal style pointers (with the exception that the set of things they can be aliased to is easily determined by the compiler). J. Giles