undefined-behavior

Is it undefined behavior to #define/#undef an identifier with special meaning?

那年仲夏 提交于 2020-01-14 07:15:49
问题 An answer to the question Disable check for override in gcc suggested using -Doverride= on the command line to disable errors for erroneous use of override, which is effectively the same as adding: #define override to the source file. My initial reaction was that this seems like undefined behavior since we are redefining a keyword but looking at the draft C++11 standard section 2.12 Keywords [lex.key] I was surprised that neither override nor final are keywords. They are covered in the

Is this use of the Effective Type rule strictly conforming?

夙愿已清 提交于 2020-01-13 19:12:22
问题 The Effective Type rule in C99 and C11 provides that storage with no declared type may be written with any type and, that storing a value of a non-character type will set the Effective Type of the storage accordingly. Setting aside the fact that INT_MAX might be less than 123456789, would the following code's use of the Effective Type rule be strictly conforming? #include <stdlib.h> #include <stdio.h> /* Performs some calculations using using int, then float, then int. If both results are

Is opening the SAME file in two different fstreams Undefined Behaviour?

一个人想着一个人 提交于 2020-01-13 02:40:08
问题 This recently asked question has raised another interesting issue, as discussed in the comments to one of its answers. To summarize: the OP there was having issues with code like that below, when subsequently attempting to read and write data from/to the two streams 'concurrently': ifstream infile; infile.open("accounts.txt"); ofstream outfile; outfile.open("accounts.txt"); Although the issue, in itself, was successfully resolved, it has raised a question to which I cannot find an

Can I use rvalue reference to temporary? Is it undefined behavior or not?

删除回忆录丶 提交于 2020-01-12 06:50:09
问题 Updating the question Why this two rvalue references examples have different behavior?: Source code: int a = 0; auto && b = a++; ++a; cout << a << b << endl; prints 20 Is it undefined behavior (UB) to use b after the a++ call? Maybe we cannot use b because it refers to a temporary? 回答1: No it is not undefined behavior (UB). It's fine - you can modify the contents of the temporary here (so long as the reference is valid for the lifetime of the temporary, in this case the bind to the rvalue

const member and assignment operator. How to avoid the undefined behavior?

喜欢而已 提交于 2020-01-12 03:58:05
问题 I answered the question about std::vector of objects and const-correctness, and received a comment about undefined behavior. I do not agree and therefore I have a question. Consider the class with const member: class A { public: const int c; // must not be modified! A(int c) : c(c) {} A(const A& copy) : c(copy.c) { } // No assignment operator }; I want to have an assignment operator but I do not want to use const_cast like in the following code from one of the answers: A& operator=(const A&

Flexible array members can lead to undefined behavior?

夙愿已清 提交于 2020-01-11 17:08:31
问题 By using flexible array members (FAMs) within structure types, are we exposing our programs to the possibility of undefined behavior? Is it possible for a program to use FAMs and still be a strictly conforming program? Is the offset of the flexible array member required to be at the end of the struct? The questions apply to both C99 (TC3) and C11 (TC1) . #include <stdio.h> #include <stdlib.h> #include <stddef.h> int main(void) { struct s { size_t len; char pad; int array[]; }; struct s *s =

What does “*((char*)-1) = 'x';” code mean?

岁酱吖の 提交于 2020-01-09 10:52:35
问题 I had a problem in reading redis source code, can anyone tell me what is the use of the last statement in the _redisAssert function in debug.c : *((char*)-1) = 'x'; 回答1: Update I found the line in debug.c mentioned in the OP and we can see from two lines above this code: redisLog(REDIS_WARNING,"(forcing SIGSEGV to print the bug report.)"); and the same code can be found in _redisPanic as well, so it looks like their way to force a SIGSEGV when an assertion fails or there is a panic. Original

What does “*((char*)-1) = 'x';” code mean?

故事扮演 提交于 2020-01-09 10:52:10
问题 I had a problem in reading redis source code, can anyone tell me what is the use of the last statement in the _redisAssert function in debug.c : *((char*)-1) = 'x'; 回答1: Update I found the line in debug.c mentioned in the OP and we can see from two lines above this code: redisLog(REDIS_WARNING,"(forcing SIGSEGV to print the bug report.)"); and the same code can be found in _redisPanic as well, so it looks like their way to force a SIGSEGV when an assertion fails or there is a panic. Original

Virtual destructor and undefined behavior

旧时模样 提交于 2020-01-08 17:42:30
问题 This question is different than ' When/why should I use a virtual destructor? '. struct B { virtual void foo (); ~B() {} // <--- not virtual }; struct D : B { virtual void foo (); ~D() {} }; B *p = new D; delete p; // D::~D() is not called Questions : Can this be classified as an undefined behavior (we are aware that ~D() is not going to be called for sure )? What if ~D() is empty. Will it affect the code in any way? Upon using new[] / delete[] with B* p; , the ~D() will certainly not get

Evaluation order between a method call and arguments in Java

梦想的初衷 提交于 2020-01-06 05:31:08
问题 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