undefined-behavior

C++ enum class: Cast to non existing entry

ⅰ亾dé卋堺 提交于 2021-02-19 04:52:35
问题 I have this situation on one Project where we have some socket-communication that mainly exchanges characters for flow-control. We cast those characters to an enum class : char in a switch. I was wondering, what might happen, if the other end sends an character that is not in our enum class. I have this mwe: enum class Foo : char { UNKNOWN, ENUM1 = 'A', ENUM2 = 'B', ENUM3 = 'C' }; char bar1() { return 'B'; } char bar2() { return 'D'; } int main() { switch((Foo)bar1()) { case Foo::UNKNOWN:std:

May I treat a 2D array as a contiguous 1D array?

一曲冷凌霜 提交于 2021-02-19 01:35:32
问题 Consider the following code: int a[25][80]; a[0][1234] = 56; int* p = &a[0][0]; p[1234] = 56; Does the second line invoke undefined behavior? How about the fourth line? 回答1: It's up to interpretation. While the contiguity requirements of arrays don't leave much to the imagination in terms of how to layout a multidimensional arrays (this has been pointed out before), notice that when you're doing p[1234] you're indexing the 1234th element of the zeroth row of only 80 columns. Some interpret

Do aliasing mutable raw pointers (*mut T) cause undefined behaviour?

我与影子孤独终老i 提交于 2021-02-18 20:59:58
问题 &mut T and &mut T results in a compilation error; this is great, it's objectively wrong to borrow mutably twice. Is *mut T and *mut T undefined behaviour or is this a perfectly valid thing to do? That is, is mutable pointer aliasing valid? What makes it even worse is that &mut T and *mut T actually compiles and works as intended, I can modify a value through the reference, the pointer, and then the reference again... but I've seen someone say that it's undefined behaviour. Yeah, "someone said

Do aliasing mutable raw pointers (*mut T) cause undefined behaviour?

♀尐吖头ヾ 提交于 2021-02-18 20:59:13
问题 &mut T and &mut T results in a compilation error; this is great, it's objectively wrong to borrow mutably twice. Is *mut T and *mut T undefined behaviour or is this a perfectly valid thing to do? That is, is mutable pointer aliasing valid? What makes it even worse is that &mut T and *mut T actually compiles and works as intended, I can modify a value through the reference, the pointer, and then the reference again... but I've seen someone say that it's undefined behaviour. Yeah, "someone said

Call member function of non-active union member

送分小仙女□ 提交于 2021-02-16 20:18:37
问题 Does calling foo in the following code leads to UB? using vec = std::array<int, 1>; struct field0 { vec data; operator int() { return data[0]; } }; union a { struct { vec data; } data; field0 x; }; void foo() { a bar; std::cin >> bar.data.data[0]; std::cout << bar.x; } According to standard, x and data have the same address, so it should be safe to cast this to vec* . Also, field0 and vec are layout-compatible, so it should be safe to inspect data via x.data or vice-versa. However, we not

Default argument and parameter promotions in C

女生的网名这么多〃 提交于 2021-02-08 14:25:04
问题 I was studying about default argument promotions and got stuck at one point. In C 2011 (ISO/IEC 9899:2011), the relevant part seem to be: §6.5.2.2 Function calls ¶6 If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the

Default argument and parameter promotions in C

余生长醉 提交于 2021-02-08 14:24:54
问题 I was studying about default argument promotions and got stuck at one point. In C 2011 (ISO/IEC 9899:2011), the relevant part seem to be: §6.5.2.2 Function calls ¶6 If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the

Destructor call for scalar types & undefined behavior [duplicate]

故事扮演 提交于 2021-02-08 02:58:34
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

Destructor call for scalar types & undefined behavior [duplicate]

走远了吗. 提交于 2021-02-08 02:58:09
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

Does forming a reference to an object constitute access?

微笑、不失礼 提交于 2021-02-07 20:30:49
问题 Does forming a reference to an object constitute access? Here's what GCC and Clang currently do: void test(int const volatile* ptr) noexcept { *ptr; // movl (%rdi), eax // Reads *ptr [[maybe_unused]] int const volatile& ref = *ptr; // Does not read *ptr } My question is specifically about the statement [[maybe_unused]] int const volatile& ref = *ptr; According to the abstract machine, does this read the value of the object pointed to by ptr ? Would this statement, in isolation, be undefined