undefined-behavior

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

落爺英雄遲暮 提交于 2019-12-18 11:06:08
问题 Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 11:05:49
问题 Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of

Undefined behavior in c/c++: i++ + ++i vs ++i + i++ [duplicate]

旧城冷巷雨未停 提交于 2019-12-18 09:45:25
问题 This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 2 years ago . Imagine that we have the code below: int i = 1; int j = i++ + ++i; I know that this is a Undefined Behavior, because before the semicolon, which is a sequence point, the value of i has been changed more than once. It means that the compiler may have two possibilities even if the precedence of operator plus is Left-to-Right: case 1) take the value

2D Array indexing - undefined behavior?

╄→гoц情女王★ 提交于 2019-12-18 08:58:33
问题 I've recently got into some pieces of code doing some questionable 2D arrays indexing operations. Considering as an example the following code sample: int a[5][5]; a[0][20] = 3; a[-2][15] = 4; a[5][-3] = 5; Are the indexing operations above subject to undefined behavior? 回答1: It's undefined behavior, and here's why. Multidimensional array access can be broken down into a series of single-dimensional array accesses. In other words, the expression a[i][j] can be thought of as (a[i])[j] .

Is it undefined behavior to take the address of an uninitialized pointer?

徘徊边缘 提交于 2019-12-18 08:25:20
问题 N1570 states that this is undefined behavior: §J.2/1 The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8). And in this case, our pointer has an indeterminate value: §6.7.9/10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then: — if it has pointer type, it is initialized to a null pointer; I

Modifying a const variable with the volatile keyword

回眸只為那壹抹淺笑 提交于 2019-12-18 06:51:56
问题 I was answering a question and made this test program. #include <stdio.h> int main() { volatile const int v = 5; int * a = &v; *a =4; printf("%d\n", v); return 0; } Without the volatile keyword the code optimizes (compiled with -O3 apple clang 4.2) the change of the var away, with it works as expected and the const variable is modified correctly. I was wondering if a more experienced C developer knows if there is a part of the standard that says this is unsafe or UB. UPDATE: @EricPostpischil

Modifying a const variable with the volatile keyword

ぃ、小莉子 提交于 2019-12-18 06:50:24
问题 I was answering a question and made this test program. #include <stdio.h> int main() { volatile const int v = 5; int * a = &v; *a =4; printf("%d\n", v); return 0; } Without the volatile keyword the code optimizes (compiled with -O3 apple clang 4.2) the change of the var away, with it works as expected and the const variable is modified correctly. I was wondering if a more experienced C developer knows if there is a part of the standard that says this is unsafe or UB. UPDATE: @EricPostpischil

Undefined behaviour of operators in XOR swap algorithm?

早过忘川 提交于 2019-12-18 06:13:44
问题 void swap(int* a, int* b) { if (a != b) *a ^= *b ^= *a ^= *b; } As the above *a ^= *b ^= *a ^= *b is just a shortcut for *a = *a ^ (*b = *b ^ (*a = *a ^ *b)) , could (e.g.) the 2nd *a be evaluated (for the XOR) just before the 3rd *a is modified (by the =)? Does it matter whether I write it in C99/C11/C++98/C++11? 回答1: The C++11 standard says: 5.17/1: The assignment operator (=) and the compound assignment operators all group right-to-left . (...) the assignment is sequenced after the value

Is `C == C++` undefined behaviour?

随声附和 提交于 2019-12-18 05:55:52
问题 A friend tells me that after: int C = anything; C == C++ will have the value true . This is intended as a joke, a rebuttal of sorts to the oft-claimed "C is not the same as C++". However, since == is not a sequence point, I argue that this is in fact undefined behavior. The program may first evaluate C++ , so that C > C++ and C == C++ are both undefined. However, C >= C++ will always evaluate as true. The same, of course, is true when the operands are flipped ( C++ <= C is always true and

Wrong results when appending vector to itself using copy and back_inserter [duplicate]

夙愿已清 提交于 2019-12-18 05:47:15
问题 This question already has answers here : Nice way to append a vector to itself (4 answers) Closed 5 years ago . Inspired by this question, asking how to append a vector to itself, my first thought was the following (and yes, I realize insert is a better option now): #include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> vec {1, 2, 3}; std::copy (std::begin (vec), std::end (vec), std::back_inserter (vec)); for (const auto &v : vec) std: