return stack[--stp]; - Which happens first? Is stp decremented or is the element returned?
If I have code like:
int pop()
{
开发者_如何学运维 return stack[--stp];
}
I know that it is doing two things. It is returning the value contained in the one-dimensional array 'stack' in element 'stp'. It is also decrementing 'stp'.
But which order does this happen in?
Does it return the value of element stp
, then decrement stp
?
Or does it decrement stp
, then return the value of the element now referred to by the decremented stp
?
If the code is:
int top()
{
return stack[stp-1];
}
Does it work any differently?
My apologies, I know this is very common coding style - I still have some trouble making sense of concise, uncommented code - even basics like this. Sorry.
It will decrement stp
, and then return the value from the array at the location of the new stp
value.
--stp
is a "prefix decrement", and it is defined to decrement the argument (stp
), and then return the new value. It has a counterpart called the "postfix decrement", stp--
, which decrements stp
and then returns the old value - so stack[stp--]
will give you the value the current stp
offset, but still decrement stp
.
Finally, your version with stack[stp-1]
will return the value from the same place as stack[--stp]
, but stp
itself will be unchanged.
Simple answer: decrement first, then the array access.
More complicated answer: this code uses the prefix decrement operator, which is pre-decrement. This means that the the operator's result value, is the value after the decrement is performed.
Contrast this with the post-decrement operator. return stack[stp--];
would also decrement stp
, but the index used would be the initial value.
For complex technical reasons which allow compilers maximum freedom to optimize, it isn't defined exactly when stp
is modified. But what is certainly defined is that the value used as the array index is the value that stp
would have, once decremented.
Your code for top()
doesn't modify stp
at all, but it's the same in the sense that the value it uses as the array index, is one less than the initial value of stp
. So if you call top()
, the value you get back is the same value you'll get next time you call pop()
.
--stp
= Pre-decrement. stp is decremented BEFORE it's used.stp--
= Post-decrement. stp is decremented AFTER it's used.
There are two versions of the decrement (and also increment) operator, like --i and i--. If you think of them as functions (which they are), the code would be (this assumes you know what operator overloading is, otherwise, see HERE).
// --i
int operator--() {
*this = *this - 1;
return *this;
}
// i--
int operator--(int i) {
int retVal = *this;
*this = *this - 1;
return retVal;
}
So, point being:
i-- returns the value BEFORE the decrement
--i returns the value AFTER the decrement
As for
int top() { return stack[stp-1]; }
This will return the index at one less than stp
. However, because you haven't assigned a new value to stp
, stp will be the same value after its done (which is probably not what you want).
return expression;
means "evaluate the expression and return the result to the caller". You cannot have anything else happen after a function has returned to its caller. It is impossible to first return to the caller and then cause some side-effect.
Even if you had written return i++;
or something, it would still have meant "evalute the expression i++
and then return the result to the caller". It just so happens that the result of i++
is the value that i
had before it was incremented, but still, i
was incremented. Simply think of i++
as (++i - 1)
.
There are two types of increment/decrement operations: prefix and postfix. When use prefix operators, the the operation is executed firstly, then the access happens. In postfix the first is the access, the second is the operation.
So, in:
--stp;
stp will be decremented and then accessed.
if the code is return stack[stp-1]
, it works differently, namely that stp is not modified.
The instruction return stack[--stp];
works int he following way:
- The expression to be returned is evaluated
- The result of the evaluation is returned
Therefore, the value of stp
is decremented.
As for your other other question, that is if
return stack[--stp];
is equivalent to
return stack[stp-1];
that depends of the nature of stp
. If it is an automatic variable, the change of stp
has no effect.
精彩评论