Skip to main content
Filter by
Sorted by
Tagged with
10 votes
3 answers
2k views

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 ...
FluidMechanics Potential Flows's user avatar
2 votes
5 answers
302 views

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 ...
rhall's user avatar
  • 118
0 votes
0 answers
104 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,904
2 votes
2 answers
119 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,449
8 votes
1 answer
866 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,548
3 votes
2 answers
306 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.8k
10 votes
1 answer
528 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
206 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
198 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
157 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
115 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
250 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
184 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
116 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
209 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
189 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
3 votes
5 answers
295 views

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 ...
Nick Reed's user avatar
  • 5,109
15 votes
1 answer
1k views

According to What are the reasons that extending the std namespace is considered undefined behavior?, adding anything to namespace std is Undefined Behavior, with some exceptions carved out, such as ...
Dominik Kaszewski's user avatar
3 votes
2 answers
171 views

The code below looks crazy, because I am interested in finding out the limits of what’s possible, and it isn’t exactly what I’m doing in my real code. It compiles without warnings and works as ...
njlarsson's user avatar
  • 2,540
6 votes
1 answer
245 views

I'm calling a 3rd-party C99 library from modern C++. The API contains a struct that ends with a flexible array member. To allocate the struct on the stack I think I need a byte buffer then cast it to ...
Peter Sutton's user avatar
  • 1,295
5 votes
5 answers
260 views

There are several source code files of a program. File file_a.c has an array in the global scope, and a function is provided that returns a pointer to its beginning: static int buffer[10]; int *...
Evgeny Ilyin's user avatar
0 votes
1 answer
251 views

I have a C function that uses va_list but needs to skip the first two arguments in here: static size_t event_type_CURSOR_POS__weight( const void * self, va_list * app ) { (void) va_arg( *app, ...
Sam's user avatar
  • 119
4 votes
2 answers
319 views

I'm trying to obtain the address of an object (the current one - *this) for use in seeding a random generator. I want to do this since I want to take advantage of ASLR for providing a bit of entropy. ...
Jesper Juhl's user avatar
  • 32.2k
2 votes
1 answer
207 views

int main() { const int length = 10000; double *A = new double[length]; double *first = A; double *last = A + length - 1; delete[] A; ptrdiff_t diff = last - first; } In C++, ...
chqrlie's user avatar
  • 153k
22 votes
1 answer
2k views

#include <stddef.h> #include <stdlib.h> int main(void) { const size_t length = 10000; double* a = malloc(length * sizeof *a); double* first = a; double* last = a + length-1; ...
Rasmus's user avatar
  • 443
5 votes
1 answer
281 views

The user defined attribute [[assumes(expression)]] Specifies that the given expression is assumed to always evaluate to true at a given point in order to allow compiler optimizations based on the ...
cigien's user avatar
  • 61.2k
14 votes
1 answer
384 views

Why does this code output garbage on GCC, like there is UB? auto data = std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; auto output = [data](this auto self, size_t i) { if (i >= 10) { ...
Sprite's user avatar
  • 4,159
4 votes
2 answers
231 views

I have seen the claim made that, in standard C, reading from an uninitialized object (specifically, an object with indeterminate representation) invokes undefined behavior. This also seems to be how ...
lateralfricative's user avatar
2 votes
1 answer
91 views

I'm writing unsafe code. It looks something like this: fn yield_value(value: &mut T) { let value_ptr = std::ptr::from_mut(value); SOME_ATOMIC_PTR.store(value_ptr, Ordering::Release); // [......
anon's user avatar
  • 697
0 votes
0 answers
47 views

I'm investigating the method reduce_bit_count() from Mojo's SIMD API. According to the documentation: Returns the total number of bits set in the SIMD vector. I expected the following behavior: 3 ...
Daniel's user avatar
  • 8,775
9 votes
1 answer
225 views

Basically, the question is in the title. Assume we have a type T, for which std::mem::needs_drop::<T>() == false. Is it UB to drop twice a value of type T? Here's an example: Consider the ...
Feanor's user avatar
  • 412
2 votes
1 answer
101 views

While reviewing some testing code, I have come across this situation: Suppose we have these two classes: class Project { public: Project(std::string path); ~Project; // [many more ...
perivesta's user avatar
  • 4,178
-1 votes
1 answer
81 views

If you bridge C or Objective-C to Swift, you can misuse _Nonnull to falsely make a guarantee to Swift that a value never holds nil. However, suppose I have this situation in Swift: var nonoptional: ...
CPlus's user avatar
  • 5,128
2 votes
2 answers
179 views

For the purpose of debugging, our project is sometimes using global volatile variables: volatile struct { uintptr_t last_read = 0; uintptr_t last_write = 0; size_t reads = 0; size_t ...
Dominik Kaszewski's user avatar
0 votes
1 answer
208 views

Consider the code example excerpted from cppref: #include <complex> #include <iostream> #include <memory> int main() { alignas(std::complex<float>) unsigned char buf[...
xmllmx's user avatar
  • 44.6k
13 votes
1 answer
1k views

#include <iostream> #include <new> struct A { int const n; void f() { new (this) A{2}; } void g() { std::cout << this->n; } void h() { ...
xmllmx's user avatar
  • 44.6k
2 votes
1 answer
177 views

Suppose I have the following: struct S { char b[256]; }; struct S *buf = malloc(128); buf->b[0] = 1; You may notice that the allocation size is smaller than the structure size, but at no ...
CPlus's user avatar
  • 5,128
2 votes
2 answers
135 views

Here's an example of the sort of code I'd like to be able to write for an unsafe Rust program I'm working on: use core::cell::UnsafeCell; fn main() { // create an UnsafeCell holding two `u64`s ...
ais523's user avatar
  • 2,449
-3 votes
1 answer
155 views

the following is my code: #include <iostream> int* foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; // Output: 5 *p = 8; std::...
Priya Chanchal's user avatar
11 votes
2 answers
1k views

Herb Sutter refers to that in Peering Forward - C++’s Next Decade - Herb Sutter - CppCon 2024, and it stresses language several times (e.g. here), so I'd like to understand how to tell it apart from ...
Enlico's user avatar
  • 30.3k
1 vote
1 answer
114 views

Consider p0593 and the following code: struct A { std::string s; }; static_assert(std::is_implicit_lifetime_v<A>); // true! // std::malloc is explicitly BLESSED by the C++23 standard. // But ...
xmllmx's user avatar
  • 44.6k
4 votes
2 answers
255 views

Consider the following excerpt from wg21.link/p0593: A call to memmove behaves as if it copies the source storage to a temporary area implicitly creates objects in the destination storage, and then ...
xmllmx's user avatar
  • 44.6k
19 votes
1 answer
1k views

At https://en.cppreference.com/w/cpp/utility/launder.html, there is an example as follows (simplified for brevity, comments mine): struct Base { virtual int transmogrify(); }; struct Derived : ...
xmllmx's user avatar
  • 44.6k
13 votes
4 answers
1k views

I am aware that not all types alias with each other in c. You cannot write to an address with an lvalue of one type, then read from it via an lvalue of another type. My question is if same system ...
Badasahog's user avatar
  • 1,025
3 votes
3 answers
245 views

In the following example, #include <print> struct C { int x; }; int main() { static C obj = [] { obj.x = 3; return obj; }(); std::print("{}", obj.x); } ...
con ko's user avatar
  • 1,508
2 votes
2 answers
192 views

I've noticed that when you bind a member function, it works even after you destroy the object you bound it to. Minimal reproducible example: #include <functional> #include <iostream> ...
PredaWnia's user avatar
1 vote
1 answer
180 views

This is not a XY question. I'm curious about what I'm asking. That's it. I'm not going to use std::memcpy() or other things to make copies of non-copyable objects anyway. I know what copyable means, ...
Enlico's user avatar
  • 30.3k
3 votes
1 answer
105 views

In CUDA I regularly exploit the fact that the hardware does not limit the width of shifts. This is unlike x86, where only the lower bits of the shift amount are taken into account. Unfortunately I ...
Johan's user avatar
  • 77.4k
1 vote
1 answer
159 views

I have been working on a toy interpreter, and I want to store all of the variables (which can be ints, doubles or other types) in a big std::vector<char>, but I want to make sure that the way ...
Arlo Taylor's user avatar

1
2 3 4 5
58