Mailinglist Archive: opensuse-programming (61 mails)

< Previous Next >
Re: [suse-programming-e] system-dependent behaviour
  • From: Colin Carter <colincarter@xxxxxxxxxxxxxx>
  • Date: Mon, 12 Sep 2005 01:03:55 +1000
  • Message-id: <200509120103.55541.colincarter@xxxxxxxxxxxxxx>
Hi Anders,
I will basically agree with you; I don't think that we disagree really, just
being a little pedantic about trivia.
And it is now 1 am here, so I must hit the sack. Good night.
Regards,
Colin
PS: Out of interest, and respect for you, I will have a look at your code.

On Sunday 11 September 2005 21:27, Anders Johansson wrote:
> On Sunday 11 September 2005 09:45, Colin Carter wrote:
> > > Parameters in C are always passed by value. If you want to have call by
> > > reference, you need to do that explicitly, by passing a pointer (in
> > > which case it's still call by value, since you're passing the value of
> > > the pointer)
> > >
> > > The exception is when you have an array or function as parameter, in
> > > which case the pointer to it is passed, but this is well defined, not
> > > willy nilly
> >
> > How about structures, character strings? See: a sometimes thing.
>
> A character string isn't a separate type, it is an array, so that is
> covered by the above exception.
>
> Structures are passed by value
>
> Try running the following example program
>
> #include <stdio.h>
>
> struct a{
> int a;
> long b;
> unsigned c;
> };
>
> void func(struct a var){
> printf("The address of var is %p\n", &var);
> }
>
> int main(void){
> struct a var2;
>
> printf("The address of var2 is %p\n",&var2);
> func(var2);
>
> return 0;
> }
>
> > Sorry, but I don't agree. Writing 'clever' code is confusing to the
> > reader. Whereas a good compiler (eg FORTRAN) will improve code
> > significantly better than most coders.
> > For example J = J + 1 has been compiled as inc J for many years
> > now, and FORTRAN has, for around thirty years, compiled 'do loops' ( K =
> > 1 to 100) as load registers (minus 1) and decrement the counter from 99
> > to -1 in registers.
> > A good FORTRAN compiler looks ahead and starts computing code in advance
> > of the time the previous values will be required. That is, it can see
> > things "coming up", and many lines of code will be computed out of
> > sequence.
> >
> > Fancy code just inhibits optimisation.
>
> Well, I didn't mean to imply that there was a real difference between x++
> and x=x+1, I'm sure there isn't a compiler out there that won't translate
> both to the same machine instruction. But the question is when they do it.
>
> If you have a construct like
>
> foo(x);
> x++;
>
> Then before the second line there is a whole function call to consider, and
> the compiler may or may not be aware of the increment, so it is entirely
> conceivable that it generates code that calls the function, and on return
> reloads x from memory to increment it. If on the other hand you had made it
>
> foo(x++);
>
> then it is much easier for the compiler to generate the "good" code that
> increments x while it's still held in memory, saving one or two
> instructions along the way. Now, I'm not saying there aren't compilers that
> see the two as equivalent, I'm sure many, perhaps even most do. But you
> can't be sure, and that is one reason I use constructs like that. Another
> is that it's more compact and, to my eyes, elegant to use the *fix
> notation. I don't think it's *that* much more difficult to read

< Previous Next >