undefined-behavior

Why does Rust disallow mutable aliasing?

梦想与她 提交于 2019-12-22 04:54:06
问题 Rust disallows this kind of code because it is unsafe: fn main() { let mut i = 42; let ref_to_i_1 = unsafe { &mut *(&mut i as *mut i32) }; let ref_to_i_2 = unsafe { &mut *(&mut i as *mut i32) }; *ref_to_i_1 = 1; *ref_to_i_2 = 2; } How can I do do something bad ( e.g. segmentation fault, undefined behavior, etc.) with multiple mutable references to the same thing? The only possible issues I can see come from the lifetime of the data. Here, if i is alive, each mutable reference to it should be

Is it okay for int** and const int** to alias?

戏子无情 提交于 2019-12-22 04:32:39
问题 It is my understanding that something like this is okay: const int ci = 42; const int *cip = &ci; int *ip = (int *)cip; int j = *ip; What about this? const int ci = 42; const int *cip = &ci; const int **cipp = &cip; int **ipp = (int **)cipp; int j = **ipp; 回答1: The expression *ipp is an lvalue of type int * , however it is being used to access an object of effective type const int * . (Namely, cip ). According to the letter of the standard, it is a strict aliasing violation: the list of

Can guaranteed UB be rejected at compile-time?

让人想犯罪 __ 提交于 2019-12-22 04:16:04
问题 Consider this program: #include <stdio.h> int main(void) { int x; while ( 1 == scanf("%d", &x) ) printf("%c\n", "hello"[x]); } The compiler must compile this successfully because the program has no UB so long as the user does not enter any numbers outside the range 0 - 4 . However, according to this thread UB can travel back in time. Now consider this program: int main(void) { printf("hello\n"); "hello"[6]; } Any invocation of this program results in undefined behaviour, and since that can

How would a heap-allocated const object differ from non-const one?

不问归期 提交于 2019-12-22 03:40:24
问题 In C++ it is possible to allocate a const object on heap: const Class* object = new const Class(); const_cast<Class*>( object )->NonConstMethod(); // UB so that attempt to write into an object will be UB. I don't get how such an object will be different from a heap-allocated object that is not declared const : const Class* object = new Class(); I mean when I allocate an object on stack it goes to automatic storage which is implementation-specific and so there might be some implementation

Is it undefined behaviour if multiple operands in a compound expression modify the same object?

陌路散爱 提交于 2019-12-22 01:23:48
问题 I vaguely remember reading somewhere that it is undefined behaviour if multiple operands in a compound expression modify the same object. I believe an example of this UB is shown in the code below however I've compiled on g++, clang++ and visual studio and all of them print out the same values and can't seem to produce unpredictable values in different compilers. #include <iostream> int a( int& lhs ) { lhs -= 4; return lhs; } int b( int& lhs ) { lhs *= 7; return lhs; } int c( int& lhs ) { lhs

Sequence points when calling functions in C and undefined/unspecified behaviour

北城以北 提交于 2019-12-21 17:37:33
问题 I'm trying to pin down my understanding of sequence points in C -- just wanted to check something. At present, I believe that (1) is undefined whereas (2) is merely unspecified, on the basis that in (2), there are sequence points after evaluating the arguments for g and h (so we're not modifying i twice between sequence points), but the order of evaluation of the arguments of f is still unspecified. Is my understanding correct? #include <stdio.h> int g(int i) { return i; } int h(int i) {

Is it safe and defined behavior to transmute between a T and an UnsafeCell<T>?

妖精的绣舞 提交于 2019-12-21 09:25:19
问题 A recent question was looking for the ability to construct self-referential structures. In discussing possible answers for the question, one potential answer involved using an UnsafeCell for interior mutability and then "discarding" the mutability through a transmute. Here's a small example of such an idea in action. I'm not deeply interested in the example itself, but it's just enough complication to require a bigger hammer like transmute as opposed to just using UnsafeCell::new and/or

Is it well-defined/legal to placement-new multiple times at the same address?

て烟熏妆下的殇ゞ 提交于 2019-12-21 07:21:24
问题 (Note: this question was motivated by trying to come up with preprocessor hackery to generate a no-op allocation to answer this other question: Macro that accept new object ...so bear that in mind!) Here's a contrived class: class foo { private: int bar; public: foo(int bar) : bar (bar) { std::cout << "construct foo #" << bar << std::endl; } ~foo() { std::cout << "destruct foo #" << bar << std::endl; } }; ...which I will allocate like this: // Note: for alignment, don't use char* buffer with

What is the right way to find the average of two values?

假装没事ソ 提交于 2019-12-21 07:17:21
问题 I recently learned that integer overflow is an undefined behavior in C (side question - is it also UB in C++?) Often in C programming you need to find the average of two values a and b . However doing (a+b)/2 can result in overflow and undefined behavior. So my question is - what is the right way to find the average of two values a and b in C? 回答1: With help from Secure Coding if (((si_b > 0) && (si_a > (INT_MAX - si_b))) || ((si_b < 0) && (si_a < (INT_MIN - si_b)))) { /* will overflow, so

Bug in quicksort example (K&R C book)?

一个人想着一个人 提交于 2019-12-21 04:34:06
问题 This quicksort is supposed to sort "v[left]...v[right] into increasing order"; copied (without comments) from The C Programming Language by K&R (Second Edition): void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left >= right) return; swap(v, left, (left + right) / 2); last = left; for (i = left+1; i <= right; i++) if (v[i] < v[left]) swap(v, ++last, i); swap(v, left, last); qsort(v, left, last-1); qsort(v, last+1, right); } I think there's a bug at