undefined-behavior

Writing to class member through const &

白昼怎懂夜的黑 提交于 2019-12-11 03:39:17
问题 In this example, is the c-style cast to int& followed by an assignment to kind of hack the interface of class A undefined behavior? class A { public: A() : x(0) { } ~A() { std::cout << x << std::endl; } const int& getX() { return x; } private: int x; }; int main() { A a; int& x = (int&)a.getX(); x = 17; std::cout << x << std::endl; } Output: 17 17 If so, what part of the standard can i refer to? Also, is there any reason why this compiles without warnings? (i tested with c++14 on cpp.sh with

Is INT_MIN subtracted from any integer considered undefined behavior?

倖福魔咒の 提交于 2019-12-11 03:16:12
问题 What if I have something like this: int a = 20; int min = INT_MIN; if(-a - min) //do something Assume that INT_MIN if positive is more than INT_MAX. Would min ever be converted by the compiler to something like -min as in -INT_MIN, which could be undefined? 回答1: You are right that unary minus applied to INT_MIN can be undefined, but this does not happen in your example. -a - min is parsed as (-a) - min . Variable min is only involved in binary subtraction, and the first operand only needs to

operation on 'i' may be undefined [duplicate]

怎甘沉沦 提交于 2019-12-10 23:38:32
问题 This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 5 years ago . I have this code to take a string of the form bla_2 and separate it: void separate(char* str, char* word, int* n) { int i = 0; while(str[i] != '_') { word[i] = str[i++]; } *n = str[++i] - '0'; } I got: warning: operation on ‘i’ may be undefined [-Wsequence-point] But I am only changing i via ++ operator, I am not assigning anything to. So, why is

Do the printf statements in this example invoke Undefined Behavior?

对着背影说爱祢 提交于 2019-12-10 21:43:30
问题 Derived from this question ... Given the declaration in line 1 of main , are the second and third printf statements considered undefined behavior because they point to locations not owned by this process? struct my_structure { int i; }; void main() { struct my_structure variable = {20}; struct my_structure *p = &variable; printf("NUMBER: %d\n", p++->i); printf("NUMBER: %d\n", p++->i); printf("NUMBER: %d\n", p++->i); } 回答1: In this case, first printf() is OK per C11 6.5.6.8 printf("NUMBER: %d

When does returning an rvalue reference result in undefined behavior?

為{幸葍}努か 提交于 2019-12-10 21:29:34
问题 In an Stack Overflow answer here, Kerrek posts the following code. Foo && g() { Foo y; // return y; // error: cannot bind ‘Foo’ lvalue to ‘Foo&&’ return std::move(y); // OK type-wise (but undefined behaviour, thanks @GMNG) } GManNickG points out that this results in undefined behavior. Kerrek adds True; you don't really return && from anything but forward and move. It's a contrived example. What confuses me is that the C++11 standard uses a function call that returns an rvalue reference as an

Array-pointer arithmetic - legal and undefined behaviour [closed]

本秂侑毒 提交于 2019-12-10 18:06:14
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I was asking myself if these line of code could produce undefined behaviour in C and in C++. I tried to answer to each point reading what the standard says about array subscripting (C 6.5.6 - 8). I didn't posted

Why/how does gcc compile the undefined behaviour in this signed-overflow test so it works on x86 but not ARM64?

牧云@^-^@ 提交于 2019-12-10 18:01:13
问题 I was self-studying CSAPP and got a strange result when I ran into a strange issue during the run of a assertion test. I'm not sure what to start this question with, so let me get the code first (file name visible in comments): // File: 2.30.c // Author: iBug int tadd_ok(int x, int y) { if ((x ^ y) >> 31) return 1; // A positive number and a negative integer always add without problem if (x < 0) return (x + y) < y; if (x > 0) return (x + y) > y; // x == 0 return 1; } // File: 2.30-test.c //

Why does “int x = 5; printf(”%d %d %d“, x==5, x=10, x==5);” in C print “0 10 0”? [duplicate]

大兔子大兔子 提交于 2019-12-10 17:46:19
问题 This question already has answers here : issue with assignment operator inside printf() (3 answers) How to understand the behaviour of printf statement in C? (5 answers) Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 7 months ago . I've been studying C for about a year now, and I came across this above when I was just playing around. I first thought maybe it's a case of assignment precedence (i.e. x=10 happens first), but then I tried printf("%d

Swapping Values with XOR [duplicate]

∥☆過路亽.° 提交于 2019-12-10 17:42:16
问题 This question already has answers here : Sequence Point - Xor Swap on Array get wrong result (1 answer) How does XOR variable swapping work? (9 answers) Closed 5 years ago . What is the difference between these two macros? #define swap(a, b) (((a) ^ (b)) && ((a) ^= (b) ^= (a) ^= (b))) Or #define swap(a, b) (((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b))) I saw the second macro here but couldn't understand why it wasn't written like the first one? Is there a special reason that I missed? 回答1:

Undefined behaviour of right shift (a >> b) when b is greater than the number of bits in a?

折月煮酒 提交于 2019-12-10 17:39:44
问题 Apparently, the behaviour of the right shift operation: a >> b is undefined in C and C++ when b >= sizeof(a)*CHAR_BIT (whereas in the normal case, the "new bits" introduced from the left due to the right shift are equal to zero). Why is this undefined behaviour better than setting the result to zero when b >= sizeof(a)*CHAR_BIT ? 回答1: We can get an idea of why languages choose undefined behavior from Why Language Designers Tolerate Undefined Behavior and it says: This answer came from two