Questions tagged [sequence-points]

Points in a 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.

A sequence point 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. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation. (from Wikipedia)

This tag should be used whenever the question is related to problems that are (eventually found to be) caused by ambiguous execution of subexpressions, which results in the difference of the expected result by novice programmers.

189 questions
4
votes
4 answers

Why does this code print 1 2 2 and not the expected 3 3 1?

Notice: this is a self-Q/A and a more visible targeting the erroneous information promoted by the book "Let us C". Also, please let's keep the c++ out of the discussion, this question is about C. I am reading the book "Let us C" by Yashwant…
4
votes
1 answer

Why is there a sequence point immediately before library functions?

Standard says that: There is a sequence point immediately before a library function returns. C17dr § 7.1.4 3. I know that there is a sequence point before actual call and after return statement (due to semicolon if there is another reason please…
Ulaş Sezgin
  • 308
  • 2
  • 10
4
votes
1 answer

Is inserting map size as value into a map undefined behavior?

edit: this question should have not been closed, if you look at the answers you will see they are totally different(old question has no mention of C++17). I was reading a PVS blog post where they mention the following…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
4
votes
1 answer

Sequencing sanity check

The context of my question is the implementation of a simplistic stack-based virtual machine. My implementation of the addition and multiplication operations looks like this: case OP_ADD: Push(Pop() + Pop()); break; case OP_MUL: Push(Pop()…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
4
votes
1 answer

Is gcc's -Wsequence-point warning flag broken?

I'm getting a warning for this line: e = strtol(++s, (char **)&s, 10); Moving the ++s to a separate statement makes the warning go away, but as far as I can tell, this warning is completely incorrect. The value of s is only used once, in the…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
4
votes
2 answers

operation on 'i' may be undefined

I have this code to take a string of the form bla_2 and separate it: void separate(char* str, char* word, int* n) { int i = 0; while(str[i] != '_') { word[i] = str[i++]; } *n = str[++i] - '0'; } I got: warning: operation on…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
4
votes
1 answer

Differences in C and C++ with sequence points and UB

I used this post Undefined Behavior and Sequence Points to document undefined behavior(UB) in a C program and it was pointed to me that C and C++ have their own divergent rules for this [sequence points]. So what are the differences between C and…
bolov
  • 72,283
  • 15
  • 145
  • 224
4
votes
5 answers

Is this "*ptr++ = *ptr + a" undefined behavior?

Well, I'm not really in serious need of this answer, I am just inquisitive. Expressions like *ptr++ = a are perfectly valid since we are operating on two objects ptr and *ptr but if i write *ptr++ = *ptr + a is it still valid ? For example consider…
whacko__Cracko
  • 6,592
  • 8
  • 33
  • 35
4
votes
1 answer

Undefined behavior and sequence point

From past few days I was trying to learn about undefined behavior. Few days ago I found a c-faq link. This helps a lot to clear many confusions, but creates an another big confusion when I read the question #3.8. After my lots of efforts to…
haccks
  • 104,019
  • 25
  • 176
  • 264
4
votes
3 answers

Does "(f(x))+g(y)" can make sure call f(x) first in C++?

And does f(x)+(g(y)) can make sure call g(y) first? I know the order in expression is undefined in many case, but in this case does parentheses work?
ShenDaowu
  • 93
  • 3
4
votes
2 answers

Is indexing a new map element and having something that reads it assigned to it undefined behaviour, or just unspecified?

After answering this question, there was a long discussion over whether the code in question was undefined behaviour or not. Here's the code: std::map word_count; word_count["a"] = word_count.count("a") == 0 ? 1 : 2; First of all,…
4
votes
2 answers

Why does increment operation like "a[i] = i++;" result in undefined behavior?

Possible Duplicate: Undefined Behavior and Sequence Points #include using namespace std; int main() { int x[3] = {}; int i=0; x[i] = i++; cout << x[0] << " " << x[1] << endl; return 0; } Codepad is giving me this: Line 9: warning:…
user52343
  • 773
  • 1
  • 7
  • 19
4
votes
2 answers

Sequence points when calling functions in C and undefined/unspecified behaviour

I'm trying to pin down my understanding of sequence points in C -- just wanted to check something. At present, I believe that (1) is undefined whereas (2) is merely unspecified, on the basis that in (2), there are sequence points after evaluating…
4
votes
1 answer

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

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; …
Misam
  • 4,320
  • 2
  • 25
  • 43
3
votes
2 answers

Argument evaluation order between chained static function calls

I am curious why there is a difference in the argument evaluation order between chained static functions and member functions. From the answers at this question I can see it is unspecified what the argument evaluation order is between such chained…
harrbharry
  • 71
  • 2