On Sunday 11 September 2005 12:25, Anders Johansson wrote:
On Sunday 11 September 2005 03:38, Colin Carter wrote:
On Sunday 11 September 2005 03:39, Anders Johansson wrote:
On Saturday 10 September 2005 17:47, Jerry Feldman wrote:
Just to add to Anders comments. According to the ANSI 89 standard, this is an unspecified operation. The issue here is when the postincrement operator is applied to s. C pushes variables from the last to the first because it may have an unknown number of parameters.
This is a completely unwarranted assumption. This may be the way any one compiler works (and it does make sense), but according to 6.5.2.2 of the 1999 standard
"The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call"
So while I'm sure you're right about any one compiler, it is very wrong to say that C does it, and suicidal to rely on it
I agree; you need to look at your own compilerS.
That's not what I said (or at least not what I meant :)
You need to look at your own code, and make sure you only code according to standards.
(Values? C passes values sometimes and addresses if it feels like it, so be careful.)
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.
The relevant sections are 6.5.2.2 and 6.7.5.3 in the 1999 standard
So: Why in hell would anybody want to pass something as dumb as s++ ?
Well, it's not s++ that's passed, it's s. Having 's++' in the parameter list is just telling the compiler that s is to be incremented by one immediately after its value has been used in the function call. This can be useful as a hint to the compiler, so it can increment s while it's still held in a register. If you have s=s+1; on the next line, a clever compiler might still be able to detect it and rearrange things, but with the postfix notation it's much easier. Especially in tight inner loops, small tricks like this can have noticeable payoffs
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. But you might already know this. :-) Regards, Colin