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.
sequence-points
198
questions
10
votes
1
answer
205
views
Can a macro be defined to provide function-like sequencing?
Consider the following C code:
const int array[100];
int i = 0;
int x;
int get_next(void) {
return array[i++];
}
int foo(void) {
return get_next() + get_next();
}
Assuming that i == 0 when ...
2
votes
0
answers
98
views
Why are there sequence point guarantees for C library functions?
I am looking at the C23 draft standard, but I think this would apply in C11 as well. There are several guarantees about sequence points in relation to function calls in C, such as before the return of ...
3
votes
3
answers
198
views
In C++ are all subexpressions of function call arguments sequenced consistently?
Consider a situation where we have the following functions:
// A function with two parameters.
void AcceptTwoInts(int, int);
// Two functions that accept an integer and return another integer.
int ...
5
votes
2
answers
153
views
Am I interpreting C order of operations correctly here?
I was puzzled by the fact that CPPReference says that postincrement’s value evaluation is sequenced before its side-effect, but preincrement has no such guarantee.
I have now come up with an example ...
0
votes
3
answers
155
views
Passing pointers to the same address as function arguments
Suppose I have a weired add function which does an addition and an increment then stores the result to somewhere else.
void add(const int* a, const int* b, int* c) {
*c = (*a)++ + *b;
}
If I pass ...
-1
votes
1
answer
162
views
Does int sum = func(1) + func(2) cause undefined behavior if func() modifies a global variable [duplicate]
Inspired by this SO post, I am wondering whether the below snippet causes UB as both add_func() and mul_func() could modify counter concurrently and in an unspecified order:
int counter = 0;
int ...
1
vote
3
answers
112
views
Formal understanding of volatile semantic
5.1.2.3 defines the following:
In the abstract machine, all expressions are evaluated as specified by
the semantics. An actual implementation need not evaluate part of an
expression if it can deduce ...
2
votes
1
answer
138
views
Different results obtained when using MSVC compiler and GCC compiler
I have this simple little code:
#include <iostream>
int Add (int x, int y) {
std::cout << "In Add(), received " <<x<< " and " <<y<< "\...
33
votes
3
answers
3k
views
Does the definition int a = 0, b = a++, c = a++; have defined behavior in C?
Does the definition int a = 0, b = a++, c = a++; have defined behavior in C?
Or almost equivalently, does the comma in an object definition introduce a sequence point as for the comma operator in ...
4
votes
1
answer
130
views
Is the output of this C code compiler dependent?
#include <stdio.h>
int main(void)
{
int a = 0, b = 1;
a = (a = 5) && (b = 0);
printf("%d", a);
printf("%d", b);
}
The value of variable a is getting ...
33
votes
3
answers
2k
views
Is the `this` argument evaluated before or after other member function arguments?
In the following code a member function set() is called on a model, which is a null pointer. This would be undefined behavior. However, the parameter of the member function is a result of another ...
0
votes
0
answers
96
views
Why does pre-increment not work for an overflow check, but post-increment does? [duplicate]
I attempted to write a function that increments an unsigned integer and checks if the result overflowed, by comparing the old value of the integer with the value after incrementing:
unsigned ...
1
vote
1
answer
85
views
Sequence point compiler warning only in gcc but not in clang
Consider the following code:
#include <tuple>
struct A {
template <typename... Types> operator std::tuple<Types...>() {
int i = 0;
return std::tuple<Types...>{Types(...
3
votes
3
answers
218
views
Will this expression evaluate to true or false (1 or 0) in C?
#include<stdio.h>
int main()
{
int a=4;
int b=4;
int c= a++ < ++b? 1 : 0;
printf ("%d",c);
}
It is known that there is a sequence point at ?, which means that ...
37
votes
3
answers
2k
views
Order of evaluation in v != std::exchange(v, predecessor(v))
I keep finding more idioms that lend themselves to std::exchange.
Today I found myself writing this in an answer:
do {
path.push_front(v);
} while (v != std::exchange(v, pmap[v]));
I like it a ...