2,890 questions
0
votes
0
answers
86
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
1
answer
92
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
828
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
274
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
512
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
201
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
197
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
152
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
112
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
238
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
183
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
115
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
204
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,...
15
votes
3
answers
2k
views
Why does a mismatching printf specifier also affect subsequent arguments?
I have a very simple C program where I am printing variables of different sizes.
#include <stdio.h>
unsigned int long long a;
unsigned int c;
int main() {
a = 0x1111111122222222;
c = ...
2
votes
3
answers
187
views
Bitwise operations act outside of type width in C; Compiler Bug or Within Spec?
The following derives from this question but is distinct
Consider the following code sample;
uint32_t *value = malloc(sizeof(uint32_t));
*value = 0xAAAABBBB;
int16_t *subset = (int16_t*) (value);
...