Tag: undefined-behavior
-
Is it undefined behaviour to use pointer after allocated memory?
10 I have the following code: uint8_t buffer[16]; uint8_t data[16]; uint8_t buffer_length = 16; uint8_t data_length = 0; memcpy(buffer + buffer_length, data, data_length); memcpy should be a no-op, because data_length is zero. However buffer + buffer_length points just outside of the allocated memory. I wonder if it could trigger some kind of undefined behaviour? Should…
-
Why is optimization forbidden if a C compiler cannot prove lack of UB?
10 If a C program has undefined behavior, anything can happen. Therefore compilers may assume that any given program does not contain UB. So, suppose our program contains the following: x += 5; /* Do something else without x in the meantime. */ x += 7; Of course, this can be optimized to /* Do…
-
Benefit of endless-loops without side effects in C++ being undefined behavior compared to C?
28 In C++ loops as for(;;) {} are undefined behavior, whereas they are not in C(?). On P2809R0. Trivial infinite loops are not Undefined Behavior, it is expressed that there are good reasons for doing so. Are there any simple examples to make that clear? c++ c loops optimization undefined-behavior Share Improve this question Follow…
-
Benefit of endless-loops without side-effects in C++ being UB compared to C?
19 In C++ loops as for(;;) {} are UB, whereas they are not in C? In https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2809r0.html it is expressed that there are good reasons for doing so. Are there any simple examples to make that clear? c++ c loops optimization undefined-behavior Share Improve this question Follow asked 17 hours ago wimalopaanwimalopaan 4,60811 gold badge1919…
-
Does the definition int a = 0, b = a++, c = a++; have defined behavior in C?
19 Does the definition int a = 0, b = a++, c = a++; have defined behavior in C? Or almost equivalently, does the , in an object definition introduce a sequence point as for the comma operator in expressions? Similar questions have been asked for C++: Does `int a = 0, b = a`…