Skip to main content
0 votes
0 answers
86 views

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 ...
Oersted's user avatar
  • 3,834
2 votes
1 answer
92 views

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 ...
ais523's user avatar
  • 2,384
8 votes
1 answer
828 views

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());...
Dmitriano's user avatar
  • 2,474
3 votes
2 answers
274 views

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 ...
Fedor's user avatar
  • 24.7k
10 votes
1 answer
512 views

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 ...
jpa's user avatar
  • 12.5k
Advice
1 vote
9 replies
201 views

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 ...
Kyle's user avatar
  • 1,116
1 vote
4 answers
197 views

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 ...
Dominik Kaszewski's user avatar
6 votes
1 answer
152 views

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 ...
luczzz's user avatar
  • 446
2 votes
1 answer
112 views

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, ...
Dominik Kaszewski's user avatar
5 votes
1 answer
238 views

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 ...
Malyutin Egor's user avatar
3 votes
1 answer
183 views

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 ...
curbalman's user avatar
0 votes
0 answers
115 views

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 ...
CygnusX1's user avatar
  • 22.1k
1 vote
2 answers
204 views

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,...
Samuel Hapak's user avatar
  • 7,284
15 votes
3 answers
2k views

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 = ...
Krishna's user avatar
  • 1,632
2 votes
3 answers
187 views

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); ...
Nil A Curious Programmer's user avatar

15 30 50 per page
1
2 3 4 5
193