undefined-behavior

Does cast away const of *this cause undefined behavior?

大兔子大兔子 提交于 2019-12-24 00:56:21
问题 The following code compiles. It seems to run fine. But would it cause any undefined behavior? I want to cast away the const of *this . This is for allowing a const my_iterator to mutate the data it points at. Test: class A { public: A(const int x) : x_(x) {} void set_x(int x) { x_ = x; } void set_x2(const int x) const { const_cast<A&>(*this).set_x(x); } int x_; }; int main() { A a(10); a.set_x2(100); } 回答1: Your example is not undefined behavior because a is not const . However, if a were

Accessing an array as a struct vs undefined behavior

故事扮演 提交于 2019-12-23 22:54:02
问题 Let's say we have this structure with 4 float values and a float array with 4 elements. Is it then undefined behavior or not to access the array as a Foo instance and change the array elements through that instance? struct Foo { float a; float b; float c; float d; }; float values[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; int main() { auto& floats = *reinterpret_cast<Foo*>(values); floats.a = 0.0f; floats.b = 0.0f; floats.c = 0.0f; floats.d = 0.0f; } Compile and run online: http://cpp.sh/6y7m 回答1: Yes,

How to be warned about pointers to out-of-scope local variables

浪尽此生 提交于 2019-12-23 21:01:07
问题 Consider the following code: #include <stdio.h> void badidea(int**); int main(void) { int* p; badidea(&p); printf("%d\n", *p); /* undefined behavior happens here: p points to x from badidea, which is now out of scope */ return 0; } void badidea(int** p) { int x = 5; *p = &x; } The intent seems to be that it will print 5 , but it actually invokes undefined behavior, due to dereferencing a pointer to an out-of-scope local variable in main . How can I find instances of this problem in a codebase

Undefined behavior when constexpr-evaluating negative bitshift?

吃可爱长大的小学妹 提交于 2019-12-23 20:51:11
问题 Consider the following snippet of code: int main(){ constexpr int x = -1; if(x >= 0){ constexpr int y = 1<<x; } } GCC 7 (and probably other versions of GCC) refuses to compile this and says: error: right operand of shift expression '(1 << -1)' is negative [-fpermissive] I can guess where this may have come from: the constexpr declaration on y makes GCC evaluate y at compile time, where it might be negative. Removing the constexpr fixes the error. However, is this undefined behavior by the

Why does this program terminate on my system but not on playground?

风格不统一 提交于 2019-12-23 19:24:09
问题 Consider this program: package main import "fmt" import "time" import "runtime" func main() { x := 0 go func() { time.Sleep(500 * time.Millisecond) x = 1 }() for x == 0 { runtime.Gosched() } fmt.Println("it works!") } Why does it terminate locally but not on Playground? Does the termination of my program rely on undefined behavior? 回答1: That code doesn't offer much guarantees. It's relying almost entirely on implementation details around undefined behavior. In most multi-threaded systems,

C++ reference to nowhere [closed]

橙三吉。 提交于 2019-12-23 18:23:21
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . Thinking of this question Why can I initialize reference member with data member before initializing the data member? as well as this Why is initialization of a new variable by itself valid? following strange code came to my mind: int main() { int &ri = ri; ri = 0; } This code

Is using the address of an uninitialized variable UB? [duplicate]

喜夏-厌秋 提交于 2019-12-23 17:50:28
问题 This question already has answers here : Is it undefined behavior to take the address of an uninitialized pointer? (5 answers) Closed 3 years ago . Is this small code UB? void Test() { int bar; printf("%p", &bar); } IMO it's not UB, but I'd like some other opinions. It simply prints the address of bar , even if bar has never been initialized. 回答1: TL:DR No, your code does not invoke UB by using anything uninitialized , as you might have thought. The address of a(ny) variable (automatic, in

Why is returning a non-initialized value considered undefined behavior?

╄→гoц情女王★ 提交于 2019-12-23 17:23:41
问题 While reading this I saw a UB that I don't understand, hoping you can clarify size_t f(int x) { size_t a; if(x) // either x nonzero or UB a = 42; return a; } I guess the UB is due to a not having an initialized value, but isn't that it's defined behavior? Meaning, f(0) will return the value held by variable a , whatever it is (I consider this to be something like rand() ). Must we know what value the code snippet returns for the code to have a well-defined-behavior? 回答1: Meaning, f(0) will

lambda: should capturing const reference by reference yield undefined behaviour?

百般思念 提交于 2019-12-23 14:03:05
问题 I just found a nasty bug in my code because I captured a const reference to a string by reference. By the time the lambda was run the original string object was already long gone and the referenced value was empty whereas the purpose was that it would contains the value of the original string, hence the bug. What baffles me is that this did not invoke a crash at runtime: after all, shouldn't this be undefined behaviour since afaik there is a dangling reference? Moreover when looking at id

lambda: should capturing const reference by reference yield undefined behaviour?

假装没事ソ 提交于 2019-12-23 14:01:38
问题 I just found a nasty bug in my code because I captured a const reference to a string by reference. By the time the lambda was run the original string object was already long gone and the referenced value was empty whereas the purpose was that it would contains the value of the original string, hence the bug. What baffles me is that this did not invoke a crash at runtime: after all, shouldn't this be undefined behaviour since afaik there is a dangling reference? Moreover when looking at id