201 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
12
votes
3
answers
2k
views
What sequence of operations could possibly have `int i = 1; f(i++, i);` invoke `f(2, 2)`?
This answer says that
i = 1;
f(i++, i)
will not evaluate to f(2, 2) since C++17.
However, I'm struggling to understand why this would have been possible before C++17.
What operations, and in what ...
3
votes
5
answers
296
views
Does unsequenced behavior repeat itself?
tl;dr
I have a program that "works", but does so in part via unsequenced behavior. I recognize that code with unsequenced behavior can technically do anything - however, it has to do ...
4
votes
2
answers
137
views
Multiple unsequenced modifications following argument evaluation - defined or undefined behavior?
The standard states that the behavior of unsequenced operations that have side effect on the same memory location is undefined [intro.execution§10].
Does the following code have undefined behavior, ...
10
votes
1
answer
233
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
129
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 ...
4
votes
3
answers
262
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
167
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
181
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
vote
2
answers
236
views
Does int sum = func(1) + func(2) cause undefined behavior if func() modifies a global variable
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
146
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
190
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
145
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 ...
32
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
125
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 ...