Output receivedCode:int main() { int l =8; printf("%d\n%d\n",l,l++); return 0; }
A8 // But why.....
B8
1. how are the arguments pushed to the stack ? isn't this right to left?
if its right to left then output A should be 9 right?
Output receivedCode:int main() { int l =8; printf("%d\n%d\n",l,l++); return 0; }
A8 // But why.....
B8
1. how are the arguments pushed to the stack ? isn't this right to left?
if its right to left then output A should be 9 right?
It's not necessary that the arguments will always be passed from right to left. It's compiler specific. So the result is actually undefined(vary from compiler to compiler).
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
The result of l++ is 8.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
My question is why the operation l++ is not executed?
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
>> With an compiler that pushes the argument right to left, shouldn't the output be 9 and 8.
That's why is called 'undefined behavior'. Consider this bit of code:
The compiler could break this down into:Code:foo( bar++ );
- or perhaps -Code:push bar call foo increment bar
Naturally then, had the code been:Code:push bar increment bar call foo
The compiler may well generate:Code:foo( bar, bar++, bar );
So in other words, it's all very unpredictable.Code:push bar push bar push bar call foo increment bar
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
thanks....Sebastiani
Compile with warnings.
In this case, the only thing that is guaranteed is that l will be incremented before entering the body of printf(), but parameter values the function is called with are undefined.
It is not advisable to increment or decrement the same variable in a single statement. Your title of this was a bit off key as well.
>> In this case, the only thing that is guaranteed is that l will be incremented before entering the body of printf()
No. There is no guarantee here whatsoever.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
I think msh is correct: there is a sequence point before control enters the function, so it should be guaranteed "that l will be incremented before entering the body of printf()".Originally Posted by Sebastiani
EDIT:
Ah, but I see that that is a moot point when in combination with the fact that there is undefined behaviour - "parameter values the function is called with are undefined" also means that l can have any value.
Last edited by laserlight; 07-08-2009 at 08:03 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> I think msh is correct: there is a sequence point before control enters the function, so it should be guaranteed "that l will be incremented before entering the body of printf()".
Ok, I see what you mean now. The variable is first copied onto the stack, then incremented, and finally the function executes. Thanks for clarifying that.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }