Prev: C/C++ query
Next: Set implementation in STL
From: Andy Venikov on 9 Aug 2010 13:17 Daniel T. wrote: > Anubhav <rkldabs(a)gmail.com> wrote: > >> int& f(){ >> static int x = 0; >> x++; >> return x; >> } >> >> int main(){ >> int x = 0; >> f() += 1; >> f() = f() + 1; >> >> cout << f(); >> } >> >> What should be the output of the code shown? >> >> VS gives 5 and gcc gives 6. > > The output is undefined. The problem here is that "f() = f() + 1" > doesn't specify which f() will be resolved first and which second. > It looks like several posters have mixed-up "unspecified results" with "undefined behavior". The order of execution of two calls to f() is unspecified. But your program invokes undefined behaviour (which is quite different) because it tries to modify the same lvalue without a sequence point. Andy. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Prasoon Saurav on 9 Aug 2010 17:58 >Since I'm pretty sure the order of evaluation here is undefined, the behavior also is undefined. If you've got a copy of the standard at hand, look at 1.9.15. Stefan,the order of evaluation is not 'undefined', it is 'unspecified' rather, however, the behaviour of the program as a whole is undefined. :) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Dave Harris on 9 Aug 2010 17:59 prasoonsaurav.nit(a)gmail.com (Prasoon Saurav) wrote (abridged): > On Aug 7, 3:18 pm, Anubhav <rkld...(a)gmail.com> wrote: > > int& f(){ > > static int x = 0; > > x++; > > return x; > > > > } > > > > int main(){ > > int x = 0; > > f() += 1; > > f() = f() + 1; > > > > cout << f(); > > > > } > > As per my opinion your code invokes Undefined Behaviour because the > order of evaluation of arguments of assignment operator is > unspecified and both right hand side and left hand side f() refer > to the same static variable. So indirectly you are trying to > modify the variable more than once between two sequence points. There is a sequence point at each function call and return, and the function invocations can't be interleaved, so I don't think x is modified twice without an intervening sequence point. -- Dave Harris, Nottingham, UK. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: chini koni on 11 Aug 2010 16:09 On Aug 7, 3:18 am, Anubhav <rkld...(a)gmail.com> wrote: > int& f(){ > static int x = 0; > x++; > return x; > > } > > int main(){ > int x = 0; > f() += 1; > f() = f() + 1; > > cout << f(); > > } > > What should be the output of the code shown? > > VS gives 5 and gcc gives 6. It depends on the compiler optimizations I think. If you make them volatile, like change int& f() { static int x = 0;... to volatile int& f() { static volatile int x = 0;.. I think the results would be more predictable. Have you tried using volatile int instead? Srinivasan -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 12 Aug 2010 03:16
On 12 Aug., 09:09, chini koni <chinik...(a)gmail.com> wrote: > On Aug 7, 3:18 am, Anubhav <rkld...(a)gmail.com> wrote: > > > int& f(){ > > static int x = 0; > > x++; > > return x; > > > } > > > int main(){ > > int x = 0; > > f() += 1; > > f() = f() + 1; > > > cout << f(); > > > } > > > What should be the output of the code shown? > > > VS gives 5 and gcc gives 6. > > It depends on the compiler optimizations I think. > If you make them volatile, like change > int& f() { > static int x = 0;... > to > volatile int& f() { > static volatile int x = 0;.. > I think the results would be more predictable. Have you tried using > volatile int instead? The described problem is unaffected by the semantics of volatile. If that "works" in your case, it is nothing, you can generally rely on. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |