undefined-behavior

Evaluation order between a method call and arguments in Java

房东的猫 提交于 2020-01-06 05:31:03
问题 Dealing with another SO question, I was wondering if the code below has an undefined behavior: if (str.equals(str = getAnotherString())) { // [...] } I tend to think the str reference from which the equals() call is made is evaluated before the further str assignment passed as argument. Is there a source about it? 回答1: This is clearly specified in the JLS Section 15.12.4: At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument

Why is the downcast in CRTP defined behaviour

安稳与你 提交于 2020-01-06 04:46:09
问题 I have used the CRTP pattern for a while, however reading answers about undefined behaviour related to downcasting I don't understand why the static_cast<Derived&>(this) , where this is of type Base<Derived>* is defined behaviour, where Derived inherits publicly from Base<Derived> . The standard is quite clear: static_cast < new_type > ( expression ) If new_type is a pointer or reference to some class D and the type of expression is a pointer or reference to its non-virtual base B, static

Is null pointer dereference undefined behavior in Objective-C?

安稳与你 提交于 2020-01-05 07:27:21
问题 In C and C++, null pointer dereference is undefined behavior. What about Objective-C? In other words, what is this code guaranteed to do? *(long*)0 = 0; Background: I wonder if this answer might trigger undefined behavior potentially causing random things like the statement being optimized out or even weirder things. Of course, I do not endorse doing this. Still, it is important to know the rules of the language. 回答1: Since Objective-C is nothing more than an object-oriented layer on top of C

Timing of lambda expression move capture

别等时光非礼了梦想. 提交于 2020-01-05 05:42:26
问题 When I use Boost.Asio, creating object such as ip::tcp::socket or deadline_timer as std::shared_ptr and copy captured it to the completion handler as lambda expression. I curious that what happens if I use move capture instead of copy capture. I think that it is dangerous. In the following example, I think that tim = std::move(tim) is evaluated before tim->async_wait . So tim no longer has valid pointer. It is my guess. In order to trace std::shared_ptr 's behavior, I created std::shared_ptr

The same instances of the same class but different behaviour. Probable UB

拜拜、爱过 提交于 2020-01-05 04:22:07
问题 #include <iostream> #include <atomic> #include <memory> template<typename T> class LockFreeQueue { public: struct CountedNode; private: std::atomic<CountedNode> head; public: struct Node{ explicit Node(const T& d) : next(CountedNode()), data(std::make_shared<T>(d)), node_counter(0) { } std::atomic<CountedNode> next; std::shared_ptr<T> data; std::atomic<unsigned> node_counter; }; struct CountedNode { CountedNode() noexcept : node(nullptr), counter(0) {} explicit CountedNode( const T& data)

Skipping switch cases via false loop is a valid operation?

北慕城南 提交于 2020-01-04 04:18:09
问题 Would this be legal code or breaking any rules? switch (expr) { do { case 6: /*...*/ if (/*...*/) break; case 7: /*...*/ } while (0); case 9: /*...*/ break; default: break; } Would this be a legal way of executing case 6 followed by case 7 but only if some conditions are met? Or would this lead into undefined behavior and lets nasal dragons come out of the switch? p.s. my Question is refering to c99. EDIT: What i want to do is the following: assume, case 9, has to be executed in everycase. If

When can I get away with not declaring int with signed?

自作多情 提交于 2020-01-04 01:05:14
问题 In C, signed integers like -1 are supposedly supposed to be declared with the keyword signed , like so: signed int i = -1; However, I tried this: signed int i = -2; unsigned int i = -2; int i = -2; and all 3 cases print out -2 with printf("%d", i); . Why? 回答1: Since you confirmed you are printing using: printf("%d", i); this is undefined behavior in the unsigned case. This is covered in the draft C99 standard section 7.19.6.1 The fprintf function which also covers printf for format specifiers

Can an implementation specify undefined behavior

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 19:14:46
问题 3.4.1 1 implementation-defined behavior unspecified behavior where each implementation documents how the choice is made Can an implementation specify that, implementation-defined behavior is undefined behavior in cases where undefined behavior is a possible outcome? For example: 6.3.1.3 Signed and unsigned integers 3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised. So as long

How will be operands inside (a += 3, 5, a) are going to dealt or caculated in order to print the value of “a”?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 17:24:16
问题 The code snippet is: int main() { int a = 1, b = 2, c = 3; printf("%d", a += (a += 3, 5, a)); } Though it displays 8 in the terminal as an output. But am not getting the concept behind it. 回答1: This is an effect how the comma operator works, the last element is the one that is used as the value of the statement. So essentially what you got here is the following: a += (a += 3, 5, a) This evaluates a+=3 first, this makes a=4 this result is discarded, then evaluate 5 then this result is

Can unsigned integer incrementation lead to undefined defined behavior?

点点圈 提交于 2020-01-03 16:00:55
问题 After reading the 32 bit unsigned multiply on 64 bit causing undefined behavior? question here on StackOverflow, I began to ponder whether typical arithmetic operations on small unsigned types could lead to undefined behavior according to the C99 standard. For example, take the following code: #include <limits.h> ... unsigned char x = UCHAR_MAX; unsigned char y = x + 1; The x variable is initialized to the maximum magnitude for the unsigned char data type. The next line is the issue: the