2,892 questions
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 ...
2
votes
5
answers
330
views
Understanding undefined behaviour
I have read about undefined behaviour for the C3 language here:
https://c3-lang.org/language-rules/undefined-behaviour/
Here is the example from the page.
Assume following code:
uint x = foo();
uint z ...
0
votes
0
answers
106
views
Modifying object representation in presence of padding bits
This question is a follow-up of std::bit_cast padding and undefined behavior. This "revival" is motivated by my answer to Accessing object storage where I proposed a function to modify a ...
2
votes
2
answers
122
views
Is it undefined behaviour to free a `static restrict` array function parameter?
I'm asking this question from the perspective of a compiler developer – I want to know whether a particular potential optimisation is valid (because all programs that the optimisation would break have ...
8
votes
1
answer
877
views
When is reinterpret_cast UB?
Does the code below contain undefined behavior (UB)?
struct A
{
int x;
char y;
};
int main()
{
std::vector<uint8_t> v(sizeof(A), 0);
A* p = reinterpret_cast<A*>(v.data());...
3
votes
2
answers
315
views
Is it ok to write `&*string.end()`?
I see many places in public repositories, where the first and last iterators of std::vector/std::string/std::string_view are converted into pointers using the combination of &* operators. In ...
10
votes
1
answer
530
views
Is this a well-defined way to access bitfield bits by index in C11
The order of bits in C11 bitfields is implementation-defined, and so is any padding in between them. This makes it difficult to access the bitfields by index at runtime, even though most ...
Advice
1
vote
9
replies
211
views
Is this array subscripting behavior really undefined in C?
unsigned int n = 1;
char* s = "X" + 1;
s[-n];
Is that third statement undefined in C23? It seems it is, as by the standard in 6.5.2.1.2:
... The definition of the subscript operator [] is ...
1
vote
4
answers
203
views
Does union "common initial sequence" include bitfields?
As far as I understand, you are allowed to access inactive members of a union, if they share a "common initial sequence" with an active one. This can be used to tag active member with a type ...
6
votes
1
answer
157
views
Is this const_cast in the context of type-erasure undefined behavior?
While looking at this example: https://github.com/PacktPublishing/Hands-On-Design-Patterns-with-CPP-Second-Edition/blob/main/Chapter06/09_function.C
which is an implementation of std::function with ...
2
votes
1
answer
116
views
Hide implementation similar to pimpl without heap allocafion
I am trying to hide some implementation behind an opaque data type. Normally, pimpl would be the preferred pattern, but it requires heap allocation which is not desirable in this context (embedded, ...
5
votes
1
answer
254
views
Is it legal to cast a repr(C) struct pointer to pointer to its first field?
In C, this is legal as far as I know (In C, does a pointer to a structure always point to its first member?).
#include <stdio.h>
typedef struct {
char *name;
int age;
} A;
typedef ...
3
votes
1
answer
186
views
Is aliasing &mut T in Cell<T> undefined behaviour?
Let's say we have a global state OK inside an UnsafeCell, and instead of unsafe { OK.get().as_mut().unwrap() } every time, we create a convenient getter get_mut. Is it UB to call get_mut inside the ...
0
votes
0
answers
116
views
Does std::is_invocable_r with void as return type lead to UB?
Today I stumbled upon weird behavior of std::is_invocable_r.
While doing research for why it is happening, I stumbled upon this question on Stack Overflow:
is_invocable_r ignoring the return parameter
...
1
vote
2
answers
213
views
Is data race between processes UB?
Is data race undefined behaviour only for two threads within same process, or is it UB also between processes?
Definition of Data Race from Rustonomicon:
Safe Rust guarantees an absence of data races,...