undefined-behavior

Is casting a pointer to const pointer and cast back to the original type undefined?

吃可爱长大的小学妹 提交于 2021-01-29 02:24:16
问题 I know casting a const pointer to non-const type might be undefined behavior, but what if the pointer is originally not const? int i = 0; int * pi = &i; const int * const_pi = const_cast<const int*>(pi); int * non_const_pi = const_cast<int*>(const_pi); *non_const_pi = 0; *non_const_pi = 1; int j = *non_const_pi; Is there's any undefined behavior? If any, where do they happen? May the compiler assume that non_const_pi is casted from a const pointer and perform no modification? 回答1: I know

Is casting a pointer to const pointer and cast back to the original type undefined?

China☆狼群 提交于 2021-01-29 02:21:30
问题 I know casting a const pointer to non-const type might be undefined behavior, but what if the pointer is originally not const? int i = 0; int * pi = &i; const int * const_pi = const_cast<const int*>(pi); int * non_const_pi = const_cast<int*>(const_pi); *non_const_pi = 0; *non_const_pi = 1; int j = *non_const_pi; Is there's any undefined behavior? If any, where do they happen? May the compiler assume that non_const_pi is casted from a const pointer and perform no modification? 回答1: I know

Is undefined behavior possible in safe Rust?

我怕爱的太早我们不能终老 提交于 2021-01-28 03:21:26
问题 Is there any way to achieve undefined behavior in Rust without using unsafe ? Of course, such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're using only the standard one. 回答1: Absolutely, but any such case is a bug with Rust or the standard libary. My favorite example is LLVM loop optimization can make safe programs crash, which actually occurs due to a poor interaction of Rust and LLVM semantics: pub fn oops() { (|| loop { drop(42) })() } Compiled

Is dereferencing a NULL pointer considered unspecified or undefined behaviour?

假如想象 提交于 2021-01-27 20:41:27
问题 The consensus of stackoverflow questions say that it is undefined behaviour. However, I recently saw a 2016 talk by Charles Bay titled: Instruction Reordering Everywhere: The C++ 'As-If" Rule and the Role of Sequence. At 37:53 he shows the following: C++ Terms Undefined Behaviour: Lack of Constraints (order of globals initialization) Unspecified Behaviour: Constraint Violation (dereferencing NULL pointer) Now I have conflicting information. Was this a typo? Has anything changed? 回答1: The

C standard regarding pointer arithmetic outside arrays

时光毁灭记忆、已成空白 提交于 2021-01-27 04:38:52
问题 I read lot of things about pointer arithmetic and undefined behavior (link, link, link, link, link). It always ends up to the same conclusion: Pointer arithmetic is well defined only on array type and between array[0] and array[array_size+1] (one element past the end is valid with regard to the C standard). My question is: Does it means that when the compiler sees a pointer arithmetic not related to any array (undefined behavior), it could emit what it want (even nothing) ? Or is it more a

C standard regarding pointer arithmetic outside arrays

 ̄綄美尐妖づ 提交于 2021-01-27 04:38:23
问题 I read lot of things about pointer arithmetic and undefined behavior (link, link, link, link, link). It always ends up to the same conclusion: Pointer arithmetic is well defined only on array type and between array[0] and array[array_size+1] (one element past the end is valid with regard to the C standard). My question is: Does it means that when the compiler sees a pointer arithmetic not related to any array (undefined behavior), it could emit what it want (even nothing) ? Or is it more a

Is accessing an element of a multidimensional array out of bounds undefined behavior?

纵然是瞬间 提交于 2021-01-27 04:13:10
问题 Pardon the confusing question title, but I was unsure how to phrase it more clearly. In C, accessing an array out of bounds is classified as undefined behavior. However, array elements are guaranteed to be laid out contiguously in memory, and the array subscript operator is syntactic sugar for pointer arithmetic (e.g x[3] == *(x + 3) ). Therefore, I would personally expect the behavior of the code below to be well-defined: int array[10][10]; int i = array[0][15]; // i == array[1][5]? If my

Does i++ invoke undefined behavior for signed types smaller than int in case of overflow?

…衆ロ難τιáo~ 提交于 2021-01-27 04:12:46
问题 It seems clear that the following code invokes undefined behavior because of arithmetic overflow: #include <limits.h> int test(void) { int i = INT_MAX; i++; /* undefined behavior */ return i; } But what about signed types smaller than int such as short or signed char ? (by smaller, I assume SCHAR_MAX < INT_MAX and SHRT_MAX < INT_MAX respectively). Which of the functions below invoke undefined behavior and why? signed char test2(void) { signed char i = SCHAR_MAX; i = i + 1; /* implementation

Does i++ invoke undefined behavior for signed types smaller than int in case of overflow?

▼魔方 西西 提交于 2021-01-27 04:10:12
问题 It seems clear that the following code invokes undefined behavior because of arithmetic overflow: #include <limits.h> int test(void) { int i = INT_MAX; i++; /* undefined behavior */ return i; } But what about signed types smaller than int such as short or signed char ? (by smaller, I assume SCHAR_MAX < INT_MAX and SHRT_MAX < INT_MAX respectively). Which of the functions below invoke undefined behavior and why? signed char test2(void) { signed char i = SCHAR_MAX; i = i + 1; /* implementation

(Where) Does clang document implementation-defined behavior?

笑着哭i 提交于 2021-01-27 01:41:14
问题 Implementation-defined behaviors in C are unspecified behaviors for which each conforming implementation must document its choice. I found such documentations easily for gcc here or Microsoft C here, but I can't find any such documentation for clang. Am I searching wrong or is there no such thing? 回答1: This ticket https://bugs.llvm.org/show_bug.cgi?id=11272 is still opened (for many years now) so it seems that clang doesn't explicitly specify implementation defined behaviour. For most cases I