sequence-points

Same code, different output in C# and C++

落爺英雄遲暮 提交于 2020-01-14 10:33:10
问题 C#: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int a = 11; int b = 2; a -= b -= a -= b += b -= a; System.Console.WriteLine(a); } } } Output:27 C++: #include "stdafx.h" #include<iostream> int _tmain(int argc, _TCHAR* argv[]) { int a = 11; int b = 2; a -= b -= a -= b += b -= a; std::cout<<a<<std::endl; return 0; } Output:76 Same code has differernt output, can somebody

Does standard C++11 guarantee that temporary object passed to a function will have been created before function call?

馋奶兔 提交于 2020-01-01 15:56:22
问题 Does standard C++11 guarantee that all 3 temporary objects have been created before the beginning performe the function? Even if temporary object passed as: object rvalue-reference passed only member of temporary object http://ideone.com/EV0hSP #include <iostream> using namespace std; struct T { T() { std::cout << "T created \n"; } int val = 0; ~T() { std::cout << "T destroyed \n"; } }; void function(T t_obj, T &&t, int &&val) { std::cout << "func-start \n"; std::cout << t_obj.val << ", " <<

Is (++i)++ undefined behavior?

寵の児 提交于 2020-01-01 09:42:36
问题 Is (++i)++ undefined behavior? Is it possible that the side effect of prefix increment happens after retrieving the incremented object for postfix increment to operate on? That would seem strange to me. My gut feeling says this is undefined in C++03 and well-defined in C++11. Am I right? 回答1: My gut feeling says this is undefined in C++03 and well-defined in C++0x. Yes you are right. The behaviour is undefined in C++03 because you are trying to modify i more than once between two sequence

Does *&++i cause undefined behaviour in C++03?

£可爱£侵袭症+ 提交于 2020-01-01 07:31:14
问题 In another answer it was stated that prior to C++11, where i is an int , then use of the expression: *&++i caused undefined behaviour. Is this true? On the other answer there was a little discussion in comments but it seems unconvincing. 回答1: It makes little sense to ask whether *&++i in itself has UB. The deferencing doesn't necessarily access the stored value (prior or new) of i , as you can see by using this as an initializer expression for a reference. Only if an rvalue conversion is

Sequence point from function call?

。_饼干妹妹 提交于 2019-12-30 08:42:02
问题 This is yet another sequence-point question, but a rather simple one: #include <stdio.h> void f(int p, int) { printf("p: %d\n", p); } int g(int* p) { *p = 42; return 0; } int main() { int p = 0; f(p, g(&p)); return 0; } Is this undefined behaviour? Or does the call to g(&p) act as a sequence point? 回答1: No. It doesn't invoke undefined behavior. It is just unspecified , as the order in which the function arguments are evaluated is unspecified in the Standard. So the output could be 0 or 42

What's wrong with this fix for double checked locking?

戏子无情 提交于 2019-12-30 06:47:53
问题 So I've seen a lot of articles now claiming that on C++ double checked locking, commonly used to prevent multiple threads from trying to initialize a lazily created singleton, is broken. Normal double checked locking code reads like this: class singleton { private: singleton(); // private constructor so users must call instance() static boost::mutex _init_mutex; public: static singleton & instance() { static singleton* instance; if(!instance) { boost::mutex::scoped_lock lock(_init_mutex); if(

Is there a sequence point between a function call returning an object and a method call on that object?

岁酱吖の 提交于 2019-12-29 06:41:10
问题 If I write f(x)->g(args, ...) can I rely on a sequence point after f(x) before the evaluation of args, ... ? I can see arguments both ways: §1.9.17 "When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body. There is also a sequence point after the copying of a returned value and before the execution of any

Is there a sequence point between a function call returning an object and a method call on that object?

半腔热情 提交于 2019-12-29 06:41:10
问题 If I write f(x)->g(args, ...) can I rely on a sequence point after f(x) before the evaluation of args, ... ? I can see arguments both ways: §1.9.17 "When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body. There is also a sequence point after the copying of a returned value and before the execution of any

sequence points in c

无人久伴 提交于 2019-12-27 10:13:30
问题 A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. What does this mean? Can somebody please explain it in simple words? 回答1: When a sequence point occurs, it basically means that you are guaranteed that all previous operations are complete. Changing a variable twice without an

C operator += Sequence point?

喜欢而已 提交于 2019-12-23 05:33:09
问题 Is this defined behaviour? *p += *p--; And, if it is, is it equivalent to { p[0] += p[0]; --p; } or to { p[-1] = p[0]; --p; } ? I'm guessing the being defined or not depends on whether += has an implicit sequence point and, if it has, my guess is that the second block should be the correct one. EDIT: I think it's not a duplicate of the suggested question because the main question there is what are sequence points and how do the affect behaviour. In my case I have clear idea of what a sequence